﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.BLL.Mtbl;
using System.Collections;
using HKEA.CMS.Core.BLL.App;
using HKEA.CMS.Core.BLL.Tbl;
using System.Text;
using System.Transactions;
namespace WebApplication
{
    public partial class M_ContactPerson : BasePage
    {
        protected string type = "";
        tblCustomer customerENT = null;
        int CustomerUID = 0;
        protected int ctuid = 0;
        int app_cpuid = 0;
        int cpuid = 0;

        int telNum = 5;
        int faxNum = 5;
        int emailNum = 5;
        int webNum = 2;
        app_tblCust_ContactPersonBLL app_cust_cpBLL = new app_tblCust_ContactPersonBLL();
        app_tblContactPersonBLL app_cpBLL = new app_tblContactPersonBLL();
        app_tblAddressBLL app_addressBLL = new app_tblAddressBLL();
        app_tblContactInfoBLL app_infoBLL = new app_tblContactInfoBLL();

        tblCust_ContactPersonBLL cust_cpBLL = new tblCust_ContactPersonBLL();
        tblContactPersonBLL cpBLL = new tblContactPersonBLL();
        tblAddressBLL addressBLL = new tblAddressBLL();
        tblContactInfoBLL infoBLL = new tblContactInfoBLL();
        tblCPerson_JobNatureBLL jobBLL = new tblCPerson_JobNatureBLL();
        tblCPerson_AttributeBLL attrBLL = new tblCPerson_AttributeBLL();
        app_tblCPerson_JobNatureBLL app_jobBLL = new app_tblCPerson_JobNatureBLL();
        app_tblCPerson_AttributeBLL app_attrBLL = new app_tblCPerson_AttributeBLL();
        tblCust_ContactPerson cust_cp = new tblCust_ContactPerson();
        tblCPerson_CommMemberBLL commBLL = new tblCPerson_CommMemberBLL();
        app_tblCPerson_CommMemberBLL app_commBLL = new app_tblCPerson_CommMemberBLL();
        app_tblMediaRoomBLL app_mediaBLL = new app_tblMediaRoomBLL();
        tblMediaRoomBLL mediaBLL = new tblMediaRoomBLL();
        app_tblMediaRoom_RelatedBLL app_relatedBLL = new app_tblMediaRoom_RelatedBLL();
        tblMediaRoomRelatedBLL tbl_relatedBLL = new tblMediaRoomRelatedBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                customerENT=GetCustomerFromSession();
                if (customerENT != null)
                {
                    hdCustomerUID.Value= hidCustomerUID.Value = customerENT.CustomerUID.ToString();
                    CustomerUID = customerENT.CustomerUID;
                    InitPage();
                }
                else
                { CustomerNotLoginOption(); }
                BindStaticInfo();
            }
        }

        private void InitPage()
        {
            type = Request.QueryString["type"];
            SetEnabled();
            AddToDropList();
            BindSalutation();
            BindArea();
            BindCountry();
            BindAttributeList();
            BindJobNatureList();
            BindCommType();
            BindCommPosition();
            initInfo();
            BindCPList();
            BindCPImg();
            string strctuid = Request.QueryString[Constant.UrlParamName.custToContactPersonUID];
            string strfrom = Request.QueryString[Constant.UrlParamName.IsFromApp];
            if (strctuid != null && strfrom != null)
            {
                lkQuit.NavigateUrl = lkAdd.NavigateUrl = "/M_ContactPerson.aspx?type=add";
                int.TryParse(strctuid, out ctuid);
                if (ctuid > 0)
                {
                    if (Request.QueryString[Constant.UrlParamName.IsFromApp].ToLower() == "true")
                    {
                        SetFormFromApp(ctuid);
                    }
                    else if (Request.QueryString[Constant.UrlParamName.IsFromApp].ToLower() == "false")
                    {
                        app_tblCust_ContactPerson app_cust_cp = new app_tblCust_ContactPerson();
                        app_cust_cp.CustContactUID = ctuid;
                        app_cust_cp.Deleted = 0;
                        app_cust_cp.CustomerUID = CustomerUID;
                        app_cust_cp=app_cust_cpBLL.GetEntity(app_cust_cp,false);
                        if (app_cust_cp != null)
                        {
                            if (app_cust_cp.Deleted == 1)
                            {
                                SetNoOption(); lblMeg.Text = ResourceManage.GetResourceString("msg200014"); return;
                            }
                            else
                            {
                                ctuid = app_cust_cp.AppCustContactUID;
                                SetFormFromApp(ctuid);
                            }
                        }
                        else
                        { SetFormFromTbl(ctuid); }
                    }
                }
                else
                { SetNoOption(); lblMeg.Text = ResourceManage.GetResourceString("msg200001"); return; }
            }
        }
        private void SetNoOption()
        {
            initInfo();
            hidAllowToOption.Value = "false";
            lkbtnSave.Enabled = false;
        }

        #region 页面加载，给文本框赋值
        protected void SetFormFromTbl(int KeyUID) // Tbl表中赋值
        {
            tblCust_ContactPerson cust_cp = new tblCust_ContactPerson();
            cust_cp.Deleted = 0;
            cust_cp.CustContactUID = ctuid;
            cust_cp.CustomerUID = CustomerUID;
            cust_cp = cust_cpBLL.GetEntity(cust_cp);
            if (cust_cp != null)
            {

                app_tblCust_ContactPerson app_cust_cp = new app_tblCust_ContactPerson();
                app_cust_cp.CustContactUID = ctuid;
                app_cust_cp.CustomerUID = CustomerUID;
                app_cust_cp = app_cust_cpBLL.GetEntity(app_cust_cp,false);
                if (app_cust_cp != null)
                {
                    Response.Redirect("/M_ContactPerson.aspx?ctcuid=" + app_cust_cp.AppCustContactUID + "&isFromApp=True");
                    return;
                }
                cpuid = cust_cp.ContactPersonUID;
                hidTblCPUID.Value = cust_cp.ContactPersonUID.ToString();
                List<int> without=new List<int>();
                hidJobNatureUID.Value = GetJobNatureFromTbl(cpuid, without);
                hidAttributeUID.Value = GetAttributeFromTbl(cpuid, without);
                SetCust_CPFromTbl(cust_cp);
                var cpENT = cust_cp.tblContactPerson;
                if (cpENT != null)
                {
                    SetCPFromTbl(cust_cp.tblContactPerson);
                    var addressENT = cpENT.tblAddress;
                    if (addressENT != null)
                        SetAddressFromTbl(addressENT);
                    var infoENT = cpENT.tblContactInfo;
                    if (infoENT != null)
                        SetInfoFromTbl(infoENT);
                }
                BindCommitteeMember();
                BindImgRelated(false);
            }
            else
            { SetNoOption(); lblMeg.Text = ResourceManage.GetResourceString("msg200014"); }
        }
        protected void SetFormFromApp(int KeyUID) //App表中赋值
        {
            app_tblCust_ContactPerson app_cust_cp = new app_tblCust_ContactPerson();
            app_cust_cp.Deleted = 0;
            app_cust_cp.CustomerUID = CustomerUID;
            app_cust_cp.AppCustContactUID = KeyUID;
            app_cust_cp = app_cust_cpBLL.GetEntity(app_cust_cp, true);
            if (app_cust_cp != null)
            {
                SetCust_CPFromApp(app_cust_cp);
                if (app_cust_cp.AppContactPersonUID > 0)
                {
                    
                    hidAppCPUID.Value = app_cust_cp.AppContactPersonUID.ToString();
                    if (app_cust_cp.ContactPersonUID > 0)
                        hidTblCPUID.Value = app_cust_cp.ContactPersonUID.ToString();
                    app_cpuid =Convert.ToInt32(app_cust_cp.AppContactPersonUID);
                    if (app_cust_cp.ContactPersonUID > 0)
                        cpuid = Convert.ToInt32(app_cust_cp.ContactPersonUID);

                    hidJobNatureUID.Value = GetJobNatureFromApp(app_cpuid);
                    hidAttributeUID.Value = GetAttributeFromApp(app_cpuid);                    
                    app_tblContactPerson app_cp = new app_tblContactPerson();
                    app_cp.AppContactPersonUID = app_cpuid;
                    app_cp.Deleted = 0;
                    app_cp = app_cpBLL.GetEntity(app_cp);
                    if (app_cp != null)
                    {
                        SetCPFromApp(app_cp);
                        if (app_cp.AppAddressUID > 0)
                        {
                            app_tblAddress app_address = new app_tblAddress();
                            app_address.AppAddressUID = Convert.ToInt32(app_cp.AppAddressUID);
                            app_address = app_addressBLL.GetAppENTByENT(app_address);
                            SetAddressFromApp(app_address);
                        }
                        if (app_cp.AppContactInfoUID > 0)
                        {
                            app_tblContactInfo app_info = new app_tblContactInfo();
                            app_info.AppContactInfoUID = Convert.ToInt32(app_cp.AppContactInfoUID);
                            app_info = app_infoBLL.GetAppENTByENT(app_info);
                            SetInfoFromApp(app_info);
                        }
                    }
                }
                BindCommitteeMember();
                BindImgRelated(false);
            }
            else
            { SetNoOption(); lblMeg.Text = ResourceManage.GetResourceString("msg200014"); }
        }

        protected void SetCust_CPFromApp(app_tblCust_ContactPerson ent) {
            txtPosition_Chi.Text = ent.Position_Chi;
            txtPosition_Eng.Text = ent.Position_Eng;
        }
        protected void SetCPFromApp(app_tblContactPerson ent)
        {
            txtChristianName.Text = ent.ChristianName;
            txtFirstName_Eng.Text = ent.FirstName_Eng;
            txtLastName_Eng.Text = ent.LastName_Eng;
            txtOtherTitle_Eng.Text = ent.OtherTitle_Eng;
            txtFirstName_Chi.Text = ent.FirstName_Chi;
            txtLastName_Chi.Text = ent.LastName_Chi;
            txtOtherTitle_Chi.Text = ent.OtherTitle_Chi;
            hidTxtHKID.Value = ent.HKID_PassportID;

            txtRelationship.Text = ent.Relationship;
            if (ent.RelatedContactPersonUID > 0)
                txtRelatedCP.Text = ent.RelatedContactPersonUID.ToString();
            txtRemark.Text = ent.Remark;
        }
        protected void SetAddressFromApp(app_tblAddress ent)
        {
            if (ent != null)
            {
                hidAddressUID.Value = ent.AddressUID.ToString();
                txtArea_Chi.Text = ent.Area_Chi;
                txtArea_Eng.Text = ent.Area_Eng;
                txtCity_Chi.Text = ent.City_Chi;
                txtCity_Eng.Text = ent.City_Eng;
                if (ent.CountryUID != null)
                {
                    if (ent.mtblCountry.AreaUID != null)
                    {
                        ddlArea.SelectedValue = ent.mtblCountry.AreaUID.ToString();
                    }
                    BindCountry();
                    ddlCountry.SelectedValue = ent.CountryUID.ToString();

                }
                txtDistrict_Chi.Text = ent.District_Chi;
                txtDistrict_Eng.Text = ent.District_Eng;
                txtFlat_Chi.Text = ent.Flat_Bldg_Chi;
                txtFlat_Eng.Text = ent.Flat_Bldg_Eng;
                txtPostCode.Text = ent.PostalCode;
                txtProvince_Chi.Text = ent.Province_Chi;
                txtProvince_Eng.Text = ent.Province_Eng;
                txtStreet_Chi.Text = ent.Street_Rd_Chi;
                txtStreet_Eng.Text = ent.Street_Rd_Eng;
                hidtxtDepartment_Chi.Value = ent.Department_Chi;
                hidtxtDepartment_Eng.Value = ent.Department_Eng;
            }
        }
        protected void SetInfoFromApp(app_tblContactInfo ent)
        {
            if (ent != null)
            {
                hidInfoUID.Value = ent.ContactInfoUID.ToString();
                #region Info
                DropDownListTel1.SelectedValue = ent.Tel1_Type.ToString();
                TextBoxTC1.Text = ent.Tel1_CountryCode;
                TextBoxTA1.Text = ent.Tel1_AreaCode;
                TextBoxT1.Text = ent.Tel1;
                CheckBoxTD1.Checked = ent.Tel1_IsDNC == 1 ? true : false;
                CheckBoxTS1.Checked = ent.Tel1_IsSubscribe == 1 ? true : false;

                if (string.IsNullOrEmpty(ent.Tel1_CountryCode) && string.IsNullOrEmpty(ent.Tel1_AreaCode) && string.IsNullOrEmpty(ent.Tel1))
                {
                    PanelT1.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel2.SelectedValue = ent.Tel2_Type.ToString();
                TextBoxTC2.Text = ent.Tel2_CountryCode;
                TextBoxTA2.Text = ent.Tel2_AreaCode;
                TextBoxT2.Text = ent.Tel2;
                CheckBoxTD2.Checked = ent.Tel2_IsDNC == 1 ? true : false;
                CheckBoxTS2.Checked = ent.Tel2_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel2_CountryCode) && string.IsNullOrEmpty(ent.Tel2_AreaCode) && string.IsNullOrEmpty(ent.Tel2))
                {
                    PanelT2.Style.Add("display", "none");
                    telNum--;
                }



                DropDownListTel3.SelectedValue = ent.Tel3_Type.ToString();
                TextBoxTC3.Text = ent.Tel3_CountryCode;
                TextBoxTA3.Text = ent.Tel3_AreaCode;
                TextBoxT3.Text = ent.Tel3;
                CheckBoxTD3.Checked = ent.Tel3_IsDNC == 1 ? true : false;
                CheckBoxTS3.Checked = ent.Tel3_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel3_CountryCode) && string.IsNullOrEmpty(ent.Tel3_AreaCode) && string.IsNullOrEmpty(ent.Tel3))
                {
                    PanelT3.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel4.SelectedValue = ent.Tel4_Type.ToString();
                TextBoxTC4.Text = ent.Tel4_CountryCode;
                TextBoxTA4.Text = ent.Tel4_AreaCode;
                TextBoxT4.Text = ent.Tel4;
                CheckBoxTD4.Checked = ent.Tel4_IsDNC == 1 ? true : false;
                CheckBoxTS4.Checked = ent.Tel4_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel4_CountryCode) && string.IsNullOrEmpty(ent.Tel4_AreaCode) && string.IsNullOrEmpty(ent.Tel4))
                {
                    PanelT4.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel5.SelectedValue = ent.Tel5_Type.ToString();
                TextBoxTC5.Text = ent.Tel5_CountryCode;
                TextBoxTA5.Text = ent.Tel5_AreaCode;
                TextBoxT5.Text = ent.Tel5;
                CheckBoxTD5.Checked = ent.Tel5_IsDNC == 1 ? true : false;
                CheckBoxTS5.Checked = ent.Tel5_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel5_CountryCode) && string.IsNullOrEmpty(ent.Tel5_AreaCode) && string.IsNullOrEmpty(ent.Tel5))
                {
                    PanelT5.Style.Add("display", "none");
                    telNum--;
                }
                if (telNum <= 0)
                    PanelT5.Style.Add("display", "true");
                #endregion

                #region Fax
                //Fax
                DropDownListFax1.SelectedValue = ent.Fax1_Type.ToString();
                TextBoxFC1.Text = ent.Fax1_CountryCode;
                TextBoxFA1.Text = ent.Fax1_AreaCode;
                TextBoxF1.Text = ent.Fax1;
                CheckBoxFD1.Checked = ent.Fax1_IsDNC == 1 ? true : false;
                CheckBoxFS1.Checked = ent.Fax1_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax1) && string.IsNullOrEmpty(ent.Fax1_AreaCode) && string.IsNullOrEmpty(ent.Fax2_CountryCode))
                {
                    PanelF1.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax2.SelectedValue = ent.Fax2_Type.ToString();
                TextBoxFC2.Text = ent.Fax2_CountryCode;
                TextBoxFA2.Text = ent.Fax2_AreaCode;
                TextBoxF2.Text = ent.Fax2;
                CheckBoxFD2.Checked = ent.Fax2_IsDNC == 1 ? true : false;
                CheckBoxFS2.Checked = ent.Fax2_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax2) && string.IsNullOrEmpty(ent.Fax2_AreaCode) && string.IsNullOrEmpty(ent.Fax2_CountryCode))
                {
                    PanelF2.Style.Add("display", "none");
                    faxNum--;
                }




                DropDownListFax3.SelectedValue = ent.Fax3_Type.ToString();
                TextBoxFC3.Text = ent.Fax3_CountryCode;
                TextBoxFA3.Text = ent.Fax3_AreaCode;
                TextBoxF3.Text = ent.Fax3;
                CheckBoxFD3.Checked = ent.Fax3_IsDNC == 1 ? true : false;
                CheckBoxFS3.Checked = ent.Fax3_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax3) && string.IsNullOrEmpty(ent.Fax3_AreaCode) && string.IsNullOrEmpty(ent.Fax3_CountryCode))
                {
                    PanelF3.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax4.SelectedValue = ent.Fax4_Type.ToString();
                TextBoxFC4.Text = ent.Fax4_CountryCode;
                TextBoxFA4.Text = ent.Fax4_AreaCode;
                TextBoxF4.Text = ent.Fax4;
                CheckBoxFD4.Checked = ent.Fax4_IsDNC == 1 ? true : false;
                CheckBoxFS4.Checked = ent.Fax4_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax4) && string.IsNullOrEmpty(ent.Fax4_AreaCode) && string.IsNullOrEmpty(ent.Fax4_CountryCode))
                {
                    PanelF4.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax5.SelectedValue = ent.Fax5_Type.ToString();
                TextBoxFC5.Text = ent.Fax5_CountryCode;
                TextBoxFA5.Text = ent.Fax5_AreaCode;
                TextBoxF5.Text = ent.Fax5;
                CheckBoxFD5.Checked = ent.Fax5_IsDNC == 1 ? true : false;
                CheckBoxFS5.Checked = ent.Fax5_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax5) && string.IsNullOrEmpty(ent.Fax5_AreaCode) && string.IsNullOrEmpty(ent.Fax5_CountryCode))
                {
                    PanelF5.Style.Add("display", "none");
                    faxNum--;
                }
                if (faxNum <= 0)
                    PanelF1.Style.Add("display", "true");
                #endregion
                #region Email
                //email 
                DropDownListEm1.SelectedValue = ent.Email1_Type.ToString();
                TextBoxEmail1.Text = ent.Email1;
                CheckBoxES1.Checked = ent.Email1_IsSubscribe == 1 ? true : false;
                CheckBoxHB1.Checked = ent.Email1_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email1))
                {
                    PanelE5.Style.Add("display", "none");
                    emailNum--;
                }



                DropDownListEm2.SelectedValue = ent.Email2_Type.ToString();
                TextBoxEmail2.Text = ent.Email2;
                CheckBoxES2.Checked = ent.Email2_IsSubscribe == 1 ? true : false;
                CheckBoxHB2.Checked = ent.Email2_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email2))
                {
                    PanelE2.Style.Add("display", "none");
                    emailNum--;
                }

                DropDownListEm3.SelectedValue = ent.Email3_Type.ToString();
                TextBoxEmail3.Text = ent.Email3;
                CheckBoxES3.Checked = ent.Email3_IsSubscribe == 1 ? true : false;
                CheckBoxHB3.Checked = ent.Email3_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email3))
                {
                    PanelE3.Style.Add("display", "none");
                    emailNum--;
                }

                DropDownListEm4.SelectedValue = ent.Email4_Type.ToString();
                TextBoxEmail4.Text = ent.Email4;
                CheckBoxES4.Checked = ent.Email4_IsSubscribe == 1 ? true : false;
                CheckBoxHB4.Checked = ent.Email4_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email4))
                {
                    PanelE4.Style.Add("display", "none");
                    emailNum--;
                }


                DropDownListEm5.SelectedValue = ent.Email5_Type.ToString();
                TextBoxEmail5.Text = ent.Email5;
                CheckBoxES5.Checked = ent.Email5_IsSubscribe == 1 ? true : false;
                CheckBoxHB5.Checked = ent.Email5_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email5))
                {
                    PanelE5.Style.Add("display", "none");
                    emailNum--;
                }
                if (emailNum <= 0)
                    PanelE1.Style.Add("display", "true");
                #endregion
                #region WebSite
                //website
                DropDownListW1.SelectedValue = ent.Website1_Type.ToString();
                TextBoxWeb1.Text = ent.Website1;
                if (string.IsNullOrEmpty(ent.Website2))
                {
                    PanelW1.Style.Add("display", "none");
                    webNum--;
                }
                DropDownListW2.SelectedValue = ent.Website2_Type.ToString();
                TextBoxWeb2.Text = ent.Website2;
                if (string.IsNullOrEmpty(ent.Website2))
                {
                    PanelW2.Style.Add("display", "none");
                    webNum--;
                }
                if (webNum <= 0)
                    PanelW1.Style.Add("display", "true");
                #endregion
            }
            else
            {
                initInfo();
            }
        }

        protected void SetCust_CPFromTbl(tblCust_ContactPerson ent)
        {
            txtPosition_Chi.Text = ent.Position_Chi;
            txtPosition_Eng.Text = ent.Position_Eng;
        }
        protected void SetCPFromTbl(tblContactPerson ent)
        {
            txtChristianName.Text = ent.ChristianName;
            txtFirstName_Eng.Text = ent.FirstName_Eng;
            txtLastName_Eng.Text = ent.LastName_Eng;
            txtOtherTitle_Eng.Text = ent.OtherTitle_Eng;
            txtFirstName_Chi.Text = ent.FirstName_Chi;
            txtLastName_Chi.Text = ent.LastName_Chi;
            txtOtherTitle_Chi.Text = ent.OtherTitle_Chi;
            hidTxtHKID.Value = ent.HKID_PassportID;

            txtRelationship.Text = ent.Relationship;
            if (ent.RelatedContactPersonUID > 0)
                txtRelatedCP.Text = ent.RelatedContactPersonUID.ToString();
            txtRemark.Text = ent.Remark;
        }
        protected void SetAddressFromTbl(tblAddress ent)
        {
            if (ent != null)
            {
                hidAddressUID.Value = ent.AddressUID.ToString();
                txtArea_Chi.Text = ent.Area_Chi;
                txtArea_Eng.Text = ent.Area_Eng;
                txtCity_Chi.Text = ent.City_Chi;
                txtCity_Eng.Text = ent.City_Eng;
                if (ent.CountryUID != null)
                {
                    if (ent.mtblCountry.AreaUID != null)
                    {
                        ddlArea.SelectedValue = ent.mtblCountry.AreaUID.ToString();
                    }
                    BindCountry();
                    ddlCountry.SelectedValue = ent.CountryUID.ToString();

                }
                txtDistrict_Chi.Text = ent.District_Chi;
                txtDistrict_Eng.Text = ent.District_Eng;
                txtFlat_Chi.Text = ent.Flat_Bldg_Chi;
                txtFlat_Eng.Text = ent.Flat_Bldg_Eng;
                txtPostCode.Text = ent.PostalCode;
                txtProvince_Chi.Text = ent.Province_Chi;
                txtProvince_Eng.Text = ent.Province_Eng;
                txtStreet_Chi.Text = ent.Street_Rd_Chi;
                txtStreet_Eng.Text = ent.Street_Rd_Eng;
                hidtxtDepartment_Chi.Value = ent.Department_Chi;
                hidtxtDepartment_Eng.Value = ent.Department_Eng;
            }
        }
        protected void SetInfoFromTbl(tblContactInfo ent)
        {
            if (ent != null)
            {
                hidInfoUID.Value = ent.ContactInfoUID.ToString();
                #region Info
                DropDownListTel1.SelectedValue = ent.Tel1_Type.ToString();
                TextBoxTC1.Text = ent.Tel1_CountryCode;
                TextBoxTA1.Text = ent.Tel1_AreaCode;
                TextBoxT1.Text = ent.Tel1;
                CheckBoxTD1.Checked = ent.Tel1_IsDNC == 1 ? true : false;
                CheckBoxTS1.Checked = ent.Tel1_IsSubscribe == 1 ? true : false;

                if (string.IsNullOrEmpty(ent.Tel1_CountryCode) && string.IsNullOrEmpty(ent.Tel1_AreaCode) && string.IsNullOrEmpty(ent.Tel1))
                {
                    PanelT1.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel2.SelectedValue = ent.Tel2_Type.ToString();
                TextBoxTC2.Text = ent.Tel2_CountryCode;
                TextBoxTA2.Text = ent.Tel2_AreaCode;
                TextBoxT2.Text = ent.Tel2;
                CheckBoxTD2.Checked = ent.Tel2_IsDNC == 1 ? true : false;
                CheckBoxTS2.Checked = ent.Tel2_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel2_CountryCode) && string.IsNullOrEmpty(ent.Tel2_AreaCode) && string.IsNullOrEmpty(ent.Tel2))
                {
                    PanelT2.Style.Add("display", "none");
                    telNum--;
                }



                DropDownListTel3.SelectedValue = ent.Tel3_Type.ToString();
                TextBoxTC3.Text = ent.Tel3_CountryCode;
                TextBoxTA3.Text = ent.Tel3_AreaCode;
                TextBoxT3.Text = ent.Tel3;
                CheckBoxTD3.Checked = ent.Tel3_IsDNC == 1 ? true : false;
                CheckBoxTS3.Checked = ent.Tel3_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel3_CountryCode) && string.IsNullOrEmpty(ent.Tel3_AreaCode) && string.IsNullOrEmpty(ent.Tel3))
                {
                    PanelT3.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel4.SelectedValue = ent.Tel4_Type.ToString();
                TextBoxTC4.Text = ent.Tel4_CountryCode;
                TextBoxTA4.Text = ent.Tel4_AreaCode;
                TextBoxT4.Text = ent.Tel4;
                CheckBoxTD4.Checked = ent.Tel4_IsDNC == 1 ? true : false;
                CheckBoxTS4.Checked = ent.Tel4_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel4_CountryCode) && string.IsNullOrEmpty(ent.Tel4_AreaCode) && string.IsNullOrEmpty(ent.Tel4))
                {
                    PanelT4.Style.Add("display", "none");
                    telNum--;
                }

                DropDownListTel5.SelectedValue = ent.Tel5_Type.ToString();
                TextBoxTC5.Text = ent.Tel5_CountryCode;
                TextBoxTA5.Text = ent.Tel5_AreaCode;
                TextBoxT5.Text = ent.Tel5;
                CheckBoxTD5.Checked = ent.Tel5_IsDNC == 1 ? true : false;
                CheckBoxTS5.Checked = ent.Tel5_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Tel5_CountryCode) && string.IsNullOrEmpty(ent.Tel5_AreaCode) && string.IsNullOrEmpty(ent.Tel5))
                {
                    PanelT5.Style.Add("display", "none");
                    telNum--;
                }
                if (telNum <= 0)
                    PanelT5.Style.Add("display", "true");
                #endregion

                #region Fax
                //Fax
                DropDownListFax1.SelectedValue = ent.Fax1_Type.ToString();
                TextBoxFC1.Text = ent.Fax1_CountryCode;
                TextBoxFA1.Text = ent.Fax1_AreaCode;
                TextBoxF1.Text = ent.Fax1;
                CheckBoxFD1.Checked = ent.Fax1_IsDNC == 1 ? true : false;
                CheckBoxFS1.Checked = ent.Fax1_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax1) && string.IsNullOrEmpty(ent.Fax1_AreaCode) && string.IsNullOrEmpty(ent.Fax2_CountryCode))
                {
                    PanelF1.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax2.SelectedValue = ent.Fax2_Type.ToString();
                TextBoxFC2.Text = ent.Fax2_CountryCode;
                TextBoxFA2.Text = ent.Fax2_AreaCode;
                TextBoxF2.Text = ent.Fax2;
                CheckBoxFD2.Checked = ent.Fax2_IsDNC == 1 ? true : false;
                CheckBoxFS2.Checked = ent.Fax2_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax2) && string.IsNullOrEmpty(ent.Fax2_AreaCode) && string.IsNullOrEmpty(ent.Fax2_CountryCode))
                {
                    PanelF2.Style.Add("display", "none");
                    faxNum--;
                }




                DropDownListFax3.SelectedValue = ent.Fax3_Type.ToString();
                TextBoxFC3.Text = ent.Fax3_CountryCode;
                TextBoxFA3.Text = ent.Fax3_AreaCode;
                TextBoxF3.Text = ent.Fax3;
                CheckBoxFD3.Checked = ent.Fax3_IsDNC == 1 ? true : false;
                CheckBoxFS3.Checked = ent.Fax3_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax3) && string.IsNullOrEmpty(ent.Fax3_AreaCode) && string.IsNullOrEmpty(ent.Fax3_CountryCode))
                {
                    PanelF3.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax4.SelectedValue = ent.Fax4_Type.ToString();
                TextBoxFC4.Text = ent.Fax4_CountryCode;
                TextBoxFA4.Text = ent.Fax4_AreaCode;
                TextBoxF4.Text = ent.Fax4;
                CheckBoxFD4.Checked = ent.Fax4_IsDNC == 1 ? true : false;
                CheckBoxFS4.Checked = ent.Fax4_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax4) && string.IsNullOrEmpty(ent.Fax4_AreaCode) && string.IsNullOrEmpty(ent.Fax4_CountryCode))
                {
                    PanelF4.Style.Add("display", "none");
                    faxNum--;
                }


                DropDownListFax5.SelectedValue = ent.Fax5_Type.ToString();
                TextBoxFC5.Text = ent.Fax5_CountryCode;
                TextBoxFA5.Text = ent.Fax5_AreaCode;
                TextBoxF5.Text = ent.Fax5;
                CheckBoxFD5.Checked = ent.Fax5_IsDNC == 1 ? true : false;
                CheckBoxFS5.Checked = ent.Fax5_IsSubscribe == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Fax5) && string.IsNullOrEmpty(ent.Fax5_AreaCode) && string.IsNullOrEmpty(ent.Fax5_CountryCode))
                {
                    PanelF5.Style.Add("display", "none");
                    faxNum--;
                }
                if (faxNum <= 0)
                    PanelF1.Style.Add("display", "true");
                #endregion
                #region Email
                //email 
                DropDownListEm1.SelectedValue = ent.Email1_Type.ToString();
                TextBoxEmail1.Text = ent.Email1;
                CheckBoxES1.Checked = ent.Email1_IsSubscribe == 1 ? true : false;
                CheckBoxHB1.Checked = ent.Email1_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email1))
                {
                    PanelE5.Style.Add("display", "none");
                    emailNum--;
                }



                DropDownListEm2.SelectedValue = ent.Email2_Type.ToString();
                TextBoxEmail2.Text = ent.Email2;
                CheckBoxES2.Checked = ent.Email2_IsSubscribe == 1 ? true : false;
                CheckBoxHB2.Checked = ent.Email2_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email2))
                {
                    PanelE2.Style.Add("display", "none");
                    emailNum--;
                }

                DropDownListEm3.SelectedValue = ent.Email3_Type.ToString();
                TextBoxEmail3.Text = ent.Email3;
                CheckBoxES3.Checked = ent.Email3_IsSubscribe == 1 ? true : false;
                CheckBoxHB3.Checked = ent.Email3_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email3))
                {
                    PanelE3.Style.Add("display", "none");
                    emailNum--;
                }

                DropDownListEm4.SelectedValue = ent.Email4_Type.ToString();
                TextBoxEmail4.Text = ent.Email4;
                CheckBoxES4.Checked = ent.Email4_IsSubscribe == 1 ? true : false;
                CheckBoxHB4.Checked = ent.Email4_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email4))
                {
                    PanelE4.Style.Add("display", "none");
                    emailNum--;
                }


                DropDownListEm5.SelectedValue = ent.Email5_Type.ToString();
                TextBoxEmail5.Text = ent.Email5;
                CheckBoxES5.Checked = ent.Email5_IsSubscribe == 1 ? true : false;
                CheckBoxHB5.Checked = ent.Email5_HardBounce == 1 ? true : false;
                if (string.IsNullOrEmpty(ent.Email5))
                {
                    PanelE5.Style.Add("display", "none");
                    emailNum--;
                }
                if (emailNum <= 0)
                    PanelE1.Style.Add("display", "true");
                #endregion
                #region WebSite
                //website
                DropDownListW1.SelectedValue = ent.Website1_Type.ToString();
                TextBoxWeb1.Text = ent.Website1;
                if (string.IsNullOrEmpty(ent.Website2))
                {
                    PanelW1.Style.Add("display", "none");
                    webNum--;
                }
                DropDownListW2.SelectedValue = ent.Website2_Type.ToString();
                TextBoxWeb2.Text = ent.Website2;
                if (string.IsNullOrEmpty(ent.Website2))
                {
                    PanelW2.Style.Add("display", "none");
                    webNum--;
                }
                if (webNum <= 0)
                    PanelW1.Style.Add("display", "true");
                #endregion
            }
            else
            {
                initInfo();
            }
        }
        protected string GetJobNatureFromApp(int AppCPUID)
        {
            string str = string.Empty;
            app_tblCPerson_JobNature app_job = new app_tblCPerson_JobNature();
            app_job.AppContactPersonUID = AppCPUID;
            var app_JobAll = app_jobBLL.GetListByENT(app_job,false);
            foreach (var temp in app_JobAll)
            {
                if (temp.Deleted == 0)
                    str += temp.JobNatureUID + ",";
            }
            var withOutNatureUIDList = app_JobAll.Where(a => a.Deleted == 1).Where(a=>a.CPerson_JobNatureUID > 0).ToList().Select(a => (int)a.CPerson_JobNatureUID).ToList();
            if (cpuid > 0)
            {
                str += GetJobNatureFromTbl(cpuid, withOutNatureUIDList);
            }
            return str;
        }
        protected string GetJobNatureFromTbl(int CpUID, List<int> withOutNatureUIDList) //withOutUID 表示：cp_JobNature表主键集合
        {
            string str = string.Empty;
            if (CpUID > 0)
            {
                tblCPerson_JobNature tbl_job = new tblCPerson_JobNature();
                tbl_job.ContactPersonUID = CpUID;
                tbl_job.Deleted = 0;
                var JobAll = jobBLL.GetList(tbl_job, withOutNatureUIDList);
                foreach (var temp in JobAll)
                {
                    str += temp.JobNatureUID + ",";
                }
            }
            return str;
            //var app_cust_cpAll = customerENT.app_tblCust_ContactPerson; //查询Member对Customer的CP所有操作的所有记录
            //customerENT.app_tblCust_ContactPerson.Load();
            ////var cust_cpAll = customerENT.tblCust_ContactPerson.Where(a => a.Deleted == 0).ToList(); //查询 Customer的CP所有未被删除的记录
            //List<int> withOutcpList = new List<int>();
            ////遍历被Memeber操作的数据，循环出tbl中的数据； 即：app表除新增数据外的其他记录
            //foreach (var temp in app_cust_cpAll.Where(a => a.CustContactUID > 0).ToList())
            //{
            //    withOutcpList.Add((int)temp.CustContactUID);
            //}
        }
        protected string GetAttributeFromApp(int AppCPUID)
        {
            string str = string.Empty;
            app_tblCPerson_Attribute app_attr = new app_tblCPerson_Attribute();
            app_attr.AppContactPersonUID = AppCPUID;
            var app_JobAll =app_attrBLL.GetListByENT(app_attr, false);
            foreach (var temp in app_JobAll)
            {
                if (temp.Deleted == 0)
                    str += temp.AttributeUID + ",";
            }
            var withOutAttrUIDList = app_JobAll.Where(a => a.Deleted == 1).Where(a => a.CPerson_AttributeUID > 0).ToList().Select(a => (int)a.CPerson_AttributeUID).ToList();
            if (cpuid > 0)
            {
                str += GetAttributeFromTbl(cpuid, withOutAttrUIDList);
            }
            return str;
        }
        protected string GetAttributeFromTbl(int CpUID, List<int> withOutAttrUIDList) //withOutUID 表示：cp_JobNature表主键集合
        {
            string str = string.Empty;
            if (CpUID > 0)
            {
                tblCPerson_Attribute tbl_attr = new tblCPerson_Attribute();
                tbl_attr.ContactPersonUID = CpUID;
                tbl_attr.Deleted = 0;
                var AttrAll = attrBLL.GetList(tbl_attr, withOutAttrUIDList);
                foreach (var temp in AttrAll)
                {
                    str += temp.AttributeUID + ",";
                }
            }
            return str;
            //var app_cust_cpAll = customerENT.app_tblCust_ContactPerson; //查询Member对Customer的CP所有操作的所有记录
            //customerENT.app_tblCust_ContactPerson.Load();
            ////var cust_cpAll = customerENT.tblCust_ContactPerson.Where(a => a.Deleted == 0).ToList(); //查询 Customer的CP所有未被删除的记录
            //List<int> withOutcpList = new List<int>();
            ////遍历被Memeber操作的数据，循环出tbl中的数据； 即：app表除新增数据外的其他记录
            //foreach (var temp in app_cust_cpAll.Where(a => a.CustContactUID > 0).ToList())
            //{
            //    withOutcpList.Add((int)temp.CustContactUID);
            //}
        }

        
        #endregion

        #region 页面第一次加载 控件初始化及数据源绑定
        private void SetEnabled()
        {
            List<TextBox> boxlist = new List<TextBox> { txtRelatedCP, txtRelationship, txtRemark };
            boxlist.ForEach(a => a.Attributes.Add("disabled", "disabled"));
        }
        private void AddToDropList()
        {
            Hashtable tellHas = Constant.InfoType.tellType;
            Hashtable FaxHas = Constant.InfoType.FaxType;
            Hashtable MailHas = Constant.InfoType.EmailType;
            Hashtable WebHas = Constant.InfoType.WebType;
            //telphone
            DropDownListTel1.DataSource = DropDownListTel2.DataSource = DropDownListTel3.DataSource = DropDownListTel4.DataSource = DropDownListTel5.DataSource = tellHas;
            DropDownListTel1.DataTextField = DropDownListTel2.DataTextField = DropDownListTel3.DataTextField = DropDownListTel4.DataTextField = DropDownListTel5.DataTextField = "value";
            DropDownListTel1.DataValueField = DropDownListTel2.DataValueField = DropDownListTel3.DataValueField = DropDownListTel4.DataValueField = DropDownListTel5.DataValueField = "key";
            DropDownListTel1.DataBind();
            DropDownListTel2.DataBind();
            DropDownListTel3.DataBind();
            DropDownListTel4.DataBind();
            DropDownListTel5.DataBind();
            //Fax
            DropDownListFax1.DataSource = DropDownListFax2.DataSource = DropDownListFax3.DataSource = DropDownListFax4.DataSource = DropDownListFax5.DataSource = FaxHas;
            DropDownListFax1.DataTextField = DropDownListFax2.DataTextField = DropDownListFax3.DataTextField = DropDownListFax4.DataTextField = DropDownListFax5.DataTextField = "value";
            DropDownListFax1.DataValueField = DropDownListFax2.DataValueField = DropDownListFax3.DataValueField = DropDownListFax4.DataValueField = DropDownListFax5.DataValueField = "key";
            DropDownListFax1.DataBind();
            DropDownListFax2.DataBind();
            DropDownListFax3.DataBind();
            DropDownListFax4.DataBind();
            DropDownListFax5.DataBind();
            //Mail
            DropDownListEm1.DataSource = DropDownListEm2.DataSource = DropDownListEm3.DataSource = DropDownListEm4.DataSource = DropDownListEm5.DataSource = MailHas;
            DropDownListEm1.DataTextField = DropDownListEm2.DataTextField = DropDownListEm3.DataTextField = DropDownListEm4.DataTextField = DropDownListEm5.DataTextField = "value";
            DropDownListEm1.DataValueField = DropDownListEm2.DataValueField = DropDownListEm3.DataValueField = DropDownListEm4.DataValueField = DropDownListEm5.DataValueField = "key";
            DropDownListEm1.DataBind();
            DropDownListEm2.DataBind();
            DropDownListEm3.DataBind();
            DropDownListEm4.DataBind();
            DropDownListEm5.DataBind();
            //WEBsit
            DropDownListW1.DataSource = DropDownListW2.DataSource = WebHas;
            DropDownListW1.DataTextField = DropDownListW2.DataTextField = "value";
            DropDownListW1.DataValueField = DropDownListW2.DataValueField = "key";
            DropDownListW1.DataBind();
            DropDownListW2.DataBind();
        }
        /// 新添加时 Contact Information 的样子
        private void initInfo()
        {
            //telephone
            PanelT1.Visible = true;
            PanelT2.Style.Add("display", "none");
            PanelT3.Style.Add("display", "none");
            PanelT4.Style.Add("display", "none");
            PanelT5.Style.Add("display", "none");

            //fax
            PanelF1.Visible = true;
            PanelF2.Style.Add("display", "none");
            PanelF3.Style.Add("display", "none");
            PanelF4.Style.Add("display", "none");
            PanelF5.Style.Add("display", "none");

            //email
            PanelE1.Visible = true;
            PanelE2.Style.Add("display", "none");
            PanelE3.Style.Add("display", "none");
            PanelE4.Style.Add("display", "none");
            PanelE5.Style.Add("display", "none");

            //website
            PanelW1.Visible = true;
            PanelW2.Style.Add("display", "none");


        }
        protected void BindSalutation()
        {
            mtblSalutation salutationENT = new mtblSalutation();
            mtblSalutationBLL salutationBLL = new mtblSalutationBLL();
            salutationENT.Deleted = 0;
            var list = salutationBLL.GetListByENT(salutationENT);
            ddlSalutation.DataSource = list;
            ddlSalutation.DataTextField = "Salutation_Eng";
            ddlSalutation.DataValueField = "SalutationUID";
            ddlSalutation.DataBind();
        }
        protected void BindArea()
        {
            mtblArea areaENT = new mtblArea();
            areaENT.Deleted = 0;
            mtblAreaBLL areaBLL = new mtblAreaBLL();
            var list = areaBLL.GetListByENT(areaENT);
            ddlArea.DataSource = list;
            ddlArea.DataTextField = "Area_Chi";
            ddlArea.DataValueField = "AreaUID";
            ddlArea.DataBind();
            BindCountry();
        }
        protected void BindCountry()
        {
            mtblCountry countryENT = new mtblCountry();
            mtblCountryBLL countryBLL = new mtblCountryBLL();
            countryENT.Deleted = 0;
            int areaUID = Convert.ToInt32(ddlArea.SelectedValue);
            countryENT.AreaUID = areaUID;
            var list = countryBLL.GetListByENT(countryENT);
            if (list.Count > 0)
            {
                ddlCountry.DataSource = list;
                ddlCountry.DataTextField = "Country_Chi";
                ddlCountry.DataValueField = "CountryUID";
                ddlCountry.DataBind();
            }
        }
        protected void BindAttributeList()
        {
            mtblAttribute attributeENT = new mtblAttribute();
            attributeENT.Deleted = 0;
            mtblAttributeBLL attributeBLL = new mtblAttributeBLL();
            var datasource = attributeBLL.GetAllByENT(attributeENT);
            ckbAttributeList.DataSource = datasource;
            ckbAttributeList.DataTextField = "FullName";
            ckbAttributeList.DataValueField = "AttributeUID";
            ckbAttributeList.DataBind();
        }
        protected void BindJobNatureList()
        {
            mtblJobNature jobNatureENT = new mtblJobNature();
            mtblJobNatureBLL jobNatureBLL = new mtblJobNatureBLL();
            jobNatureENT.Deleted = 0;
            var datasource = jobNatureBLL.GetListByENT(jobNatureENT);
            ckbJobNatureList.DataSource = datasource;
            ckbJobNatureList.DataTextField = "JobNature_Eng";
            ckbJobNatureList.DataValueField = "JobNatureUID";
            ckbJobNatureList.DataBind();
        }
        protected void BindCommType()
        {
            mtblCommType typeENT = new mtblCommType();
            mtblCommTypeBLL typeBLL = new mtblCommTypeBLL();
            typeENT.Deleted = 0;
            var source = typeBLL.GetListByENT(typeENT);
            ddlCommType.DataSource = source;
            ddlCommType.DataTextField = "CommType_Eng";
            ddlCommType.DataValueField = "CommTypeUID";
            ddlCommType.DataBind();
        }
        protected void BindCommPosition()
        {
            mtblCommPosition commPositionENT = new mtblCommPosition();
            mtblCommPositionBLL commpositionBLL = new mtblCommPositionBLL();
            commPositionENT.Deleted = 0;
            var source = commpositionBLL.GetListByENT(commPositionENT);
            ddlCommPosition.DataSource = source;
            ddlCommPosition.DataTextField = "CommPosition_Eng";
            ddlCommPosition.DataValueField = "CommPositionUID";
            ddlCommPosition.DataBind();
        }
        protected void BindCPList()
        {
            var app_cust_cpAll = customerENT.app_tblCust_ContactPerson; //查询Member对Customer的CP所有操作的所有记录
            customerENT.app_tblCust_ContactPerson.Load();
            //var cust_cpAll = customerENT.tblCust_ContactPerson.Where(a => a.Deleted == 0).ToList(); //查询 Customer的CP所有未被删除的记录
            List<int> withOutcpList = new List<int>();
            //遍历被Memeber操作的数据，循环出tbl中的数据； 即：app表除新增数据外的其他记录
            foreach (var temp in app_cust_cpAll.Where(a => a.CustContactUID > 0).ToList())  
            {
                withOutcpList.Add((int)temp.CustContactUID);
            }
            List<M_ContactPersonListDetail> listValue = new List<M_ContactPersonListDetail>();
            List<M_ContactPersonListDetail>  tblData= cust_cpBLL.GetCPListDetail(withOutcpList, customerENT.CustomerUID);
            List<M_ContactPersonListDetail> appData = app_cust_cpBLL.GetListByCustUID(customerENT.CustomerUID);
            listValue.AddRange(tblData);
            listValue.AddRange(appData);
            rptCPList.DataSource = listValue;
            rptCPList.DataBind();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "AverageBrand();", true);
        }
        protected void BindCPImg()
        {
            app_tblMediaRoom app_media = new app_tblMediaRoom();
            app_media.CustomerUID = CustomerUID;
            app_media.MediaType = Constant.MediaType.image;
            app_media.Category = Constant.MediaRoomCategory.contactperson_photo;
            var appALLList = app_mediaBLL.GetList(app_media, false);
            List<int> withOutList = new List<int>();
            foreach (var temp in appALLList.Where(a => a.TblKeyUID > 0).ToList())
            {
                withOutList.Add(temp.TblKeyUID);
            }
            var appList = appALLList.Where(a=>a.IsDeleted==0).ToList();

            tblMediaRoom mediaroomENT = new tblMediaRoom();
            tblMediaRoomBLL mediaroomBLL = new tblMediaRoomBLL();
            mediaroomENT.CustomerUID = Convert.ToInt32(CustomerUID);
            mediaroomENT.MediaType = Constant.MediaType.image;
            mediaroomENT.Category = Constant.MediaRoomCategory.contactperson_photo;
            var tblList = mediaroomBLL.GetList(mediaroomENT, withOutList);
            var ListSource = new List<MediaRoomList>();
            ListSource.AddRange(appList);
            ListSource.AddRange(tblList);
            rptImgList.DataSource = ListSource;
            rptImgList.DataBind();
        }
        protected void BindImgRelated(bool isPostBack)
        {
            //TODO 图片关联的新增、修改、删除
            List<M_MediaRoomRelatedDetail> appAllList = new List<M_MediaRoomRelatedDetail>();
            List<M_MediaRoomRelatedDetail> listValue = new List<M_MediaRoomRelatedDetail>();
            List<M_MediaRoomRelatedDetail> appList = new List<M_MediaRoomRelatedDetail>();
            List<M_MediaRoomRelatedDetail> tblList = new List<M_MediaRoomRelatedDetail>();
            List<int> withOutList = new List<int>();
            if (app_cpuid > 0)
            {
                app_tblMediaRoom_Related app_related = new app_tblMediaRoom_Related();
                app_tblMediaRoom_RelatedBLL app_relatedBLL = new app_tblMediaRoom_RelatedBLL();
                app_related.RelatedUID = cpuid;
                app_related.AppRelatedUID = app_cpuid;
                app_related.Category = Constant.MediaRoomCategory.contactperson_photo;
                appAllList = app_relatedBLL.GetList(app_related, false);
                appList = appAllList.Where(a => a.IsDeleted == 0).ToList();
                foreach (var temp in appAllList.Where(a => a.TblMediaRelatedUID > 0).ToList())
                {
                    withOutList.Add(temp.TblMediaRelatedUID);
                }
            }
            if (cpuid > 0)
            {

                tblMediaRoom_Related tbl_related = new tblMediaRoom_Related();
                tbl_related.RelatedUID = cpuid;
                tbl_related.Category = Constant.MediaRoomCategory.contactperson_photo;
                tblMediaRoomRelatedBLL tbl_relatedBLL = new tblMediaRoomRelatedBLL();
                tblList = tbl_relatedBLL.GetList(tbl_related, withOutList);
            }

            listValue.AddRange(appList);
            listValue.AddRange(tblList);
            var tempENT = listValue.FirstOrDefault();
            var source = new List<M_MediaRoomRelatedDetail>();
            var ent = listValue.FirstOrDefault(); //只允许一个
            if (ent != null)
            {
                hidMediaKeyUID.Value = ent.KeyMediaUID.ToString();
                hidMediaRelatedKeyUID.Value = ent.KeyMediaRelatedUID.ToString();
                source.Add(ent);
            }
            if (!isPostBack)
            {
                rptRelatedImgList.DataSource = source;
                rptRelatedImgList.DataBind();
            }
        }
        //protected void BindCommitteeMember()
        protected List<CommitteeMemberList> BindCommitteeMember()
        {
            List<CommitteeMemberList> appAllList = new List<CommitteeMemberList>();
            List<CommitteeMemberList> listValue = new List<CommitteeMemberList>();
            if (app_cpuid > 0 || cpuid > 0)
            {
                app_tblCPerson_CommMember app_comm = new app_tblCPerson_CommMember();
                app_comm.AppContactPersonUID = app_cpuid;
                app_comm.ContactPersonUID = cpuid;
                appAllList= app_commBLL.GetList(app_comm,false);
            }
            List<CommitteeMemberList> appData = appAllList.Where(a => a.IsDeleted == 0).ToList();

            List<int> withOutList = new List<int>();
            foreach (var temp in appAllList.Where(a => a.TblKeyUID > 0).ToList())
            {
                withOutList.Add(temp.TblKeyUID);
            }
            List<CommitteeMemberList> tblData = new List<CommitteeMemberList>();
            if (cpuid > 0)
            {
                tblData= commBLL.GetListByCPUID(cpuid,withOutList);
            }
            listValue.AddRange(tblData);
            listValue.AddRange(appData);
            rptCommitteeMember.DataSource = listValue;
            rptCommitteeMember.DataBind();
            return listValue;
        }
        #endregion
        protected void Update()
        {
            string strIsFromApp = Request.QueryString[Constant.UrlParamName.IsFromApp];
            if (!string.IsNullOrEmpty(strIsFromApp))
            {
                if (strIsFromApp.ToLower().Trim() == "true")
                    UpdateApp();
                else 
                    AddToAppFromTbl();
            }
        }
        /// <summary>
        /// 修改App中的记录
        /// </summary>
        protected void UpdateApp()
        {
            #region 修改
            app_tblContactInfo app_info = new app_tblContactInfo();
            app_tblAddress app_address = new app_tblAddress();
            app_tblCust_ContactPerson app_cust_cp = new app_tblCust_ContactPerson();
            app_tblContactPerson app_cp = new app_tblContactPerson();
            app_cust_cp.AppCustContactUID = ctuid;
            app_cust_cp.Deleted = 0;
            app_cust_cp = app_cust_cpBLL.GetEntity(app_cust_cp, true);
            if (app_cust_cp != null)
            {
                if (app_cust_cp.CustomerUID == CustomerUID)
                {
                    //获取实体
                    app_cpuid = (int)app_cust_cp.AppContactPersonUID;
                    cpuid = (int)app_cust_cp.ContactPersonUID;
                    app_cust_cp = GetApp_cust_cpENT(app_cust_cp);
                    app_cp.AppContactPersonUID = app_cust_cp.AppContactPersonUID ?? 0;
                    app_cp.Deleted = 0;
                    app_cp = app_cpBLL.GetEntity(app_cp);
                    if (app_cp != null)
                        app_cp = GetApp_cpENT(app_cp);
                    if (app_cp.AppContactInfoUID > 0)
                    {
                        app_info.AppContactInfoUID = (int)app_cp.AppContactInfoUID;
                        app_info = app_infoBLL.GetAppENTByENT(app_info);
                        app_info = getApp_infoENT(app_info);
                    }
                    if (app_cp.AppAddressUID > 0)
                    {
                        app_address.AppAddressUID = (int)app_cp.AppAddressUID;
                        app_address = app_addressBLL.GetAppENTByENT(app_address);
                        app_address = GetApp_AddressENT(app_address);
                    }
                    #region
                    //Member 相对于 上一次的添加记录(未审核期间的操作)；AddJob
                    //AddJob分两种情况：1、该CP在tbl没有的记录；(直接添加) 
                    //2、该CP在tbl存在，且上一次操作时被标记为删除的记录(删除app表中的该记录；表示该记录无任何修改；并记入Log表)

                    //Member相对于上一次的删除记录(未审核期间的操作)；DeleteJob 
                    //DeleteJob分两种情况：1、该CP在tbl没有的记录(上一次操作新增的) 直接删除记录；并记入Log表；
                    //2、存在于tbl表的记录； 新增一条记录到app，将deleted标记为1
                    //因此，综上所述， 可分分离数据(需要被新增列表、需要被物理删除的列表、删除标记需要被修改成1的列表)，最后数据库保存；
                    #endregion
                    List<int> ToDeletedJob = new List<int>(); //本地需要被物理删除的记录 (App表主键)
                    List<app_tblCPerson_JobNature> AddToDeletedJob = new List<app_tblCPerson_JobNature>(); //本次需要被新增到app做删除标记的记录(JobNatureUID)
                    List<app_tblCPerson_JobNature> AddJob = new List<app_tblCPerson_JobNature>(); //本次需要被新增的记录(JobNatureUID)
                    DetchJob(ToDeletedJob, AddToDeletedJob, AddJob);

                    List<int> ToDeletedAttribute = new List<int>(); //本地需要被物理删除的记录 (App表主键)
                    List<app_tblCPerson_Attribute> AddToDeletedAttribute = new List<app_tblCPerson_Attribute>(); //本次需要被新增到app做删除标记的记录(JobNatureUID)
                    List<app_tblCPerson_Attribute> AddAttribute = new List<app_tblCPerson_Attribute>(); //本次需要被新增的记录(JobNatureUID)
                    DetchAttr(ToDeletedAttribute, AddToDeletedAttribute, AddAttribute);

                    TransactionOptions options = new TransactionOptions();
                    options.Timeout = new TimeSpan(0, 5, 0);
                    options.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
                    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))
                    {
                        app_infoBLL.UpdateENT(app_info);
                        app_addressBLL.UpdateENT(app_address);
                        app_cpBLL.UpdateENT(app_cp);
                        app_jobBLL.SaveList(ToDeletedJob, AddJob, AddToDeletedJob);
                        app_attrBLL.SaveList(ToDeletedAttribute, AddToDeletedAttribute, AddAttribute);
                        app_cust_cpBLL.UpdateENT(app_cust_cp);
                        SaveRelatedImg();
                        BindImgRelated(false);
                        BindCPList();                        
                        try { scope.Complete();
                        lblMeg.Text = ResourceManage.GetResourceString("msg200004");
                        }
                        catch (Exception er)
                        { lblMeg.Text = ResourceManage.GetResourceString("msg200003"); }
                    }
                }
                else
                { lblMeg.Text = ResourceManage.GetResourceString("msg200013"); }
            }
            #endregion
            else
            { lblMeg.Text = ResourceManage.GetResourceString("msg200014"); }
        }
        /// <summary>
        /// tbl中数据修改，保存到app,实则为添加app记录； 与member点add按钮进行添加不同支持在于：前者有且必须有tbl的关联字段；后者无tbl的关联字段
        /// </summary>
        protected void AddToAppFromTbl()
        {
            cust_cp.CustomerUID = CustomerUID;
            cust_cp.CustContactUID = ctuid;
            cust_cp.Deleted = 0;
            cust_cp = cust_cpBLL.GetEntity(cust_cp);
            if (cust_cp == null)
            {
                lblMeg.Text = ResourceManage.GetResourceString("msg200013");
                return;
            }
            else
            {
                app_tblCust_ContactPerson app_cust_cp = new app_tblCust_ContactPerson();
                app_cust_cp.CustContactUID = ctuid;
                app_cust_cp.CustomerUID = CustomerUID;
                var tempENT=app_cust_cpBLL.GetEntity(app_cust_cp, false);
                if (tempENT == null)
                {
                    Add();
                }
                else
                {
                    if (app_cust_cp.ContactPersonUID > 0)
                        cpuid = (int)app_cust_cp.ContactPersonUID;
                    if (tempENT.Deleted == 1)
                    {
                        lblMeg.Text = ResourceManage.GetResourceString("msg200014");
                        return;
                    }
                    else
                    {
                        ctuid = tempENT.AppCustContactUID;
                        UpdateApp();
                    }
                }
            }
        }
        protected void Add()
        {
            int tbl_cust_cpUID = 0;
            int tbl_cpUID = 0;
            int tbl_addressUID = 0;
            int tbl_infoUID = 0;
            int tbl_relatedcpUID = 0;
            int tbl_activityUID = 0;
            int tbl_addressUID_cp = 0;
            int tbl_addressUID_Cust = 0;
            int tbl_addressFollowCust = 0;
            int tbl_infoUID_cp = 0;
            int tbl_infoUID_Cust = 0;
            int tbl_infoFollowCust = 0;
            if (ctuid > 0) //ctuid > 0 表示被添加到app表中的数据为tbl存在的数据
            {
                if (cust_cp != null)
                {
                    #region 数据来源与tbl，则取出关联值存放到变量中
                    tbl_cust_cpUID = cust_cp.CustContactUID;
                    tbl_cpUID = cust_cp.ContactPersonUID;
                    var tblcp = cust_cp.tblContactPerson;
                    if (tblcp != null)
                    {
                        cpuid = tblcp.ContactPersonUID;
                        if (tblcp.AddressUID > 0)
                            tbl_addressUID = (int)tblcp.AddressUID;
                        if (tblcp.ContactInfoUID > 0)
                            tbl_infoUID = (int)tblcp.ContactInfoUID;
                        if (tblcp.RelatedContactPersonUID > 0)
                            tbl_relatedcpUID = (int)tblcp.RelatedContactPersonUID;
                        if (tblcp.ActivityUID > 0)
                            tbl_activityUID = (int)tblcp.ActivityUID;
                        if (tblcp.AddressUID_CP > 0)
                            tbl_addressUID_cp = (int)tblcp.AddressUID_CP;
                        if (tblcp.AddressUID_Cust > 0)
                            tbl_addressUID_Cust = (int)tblcp.AddressUID_Cust;
                        if (tblcp.AddressFollowCust > 0)
                            tbl_addressFollowCust = (int)tblcp.AddressFollowCust;
                        if (tblcp.ContactInfoUID_CP > 0)
                            tbl_infoUID_cp = (int)tblcp.ContactInfoUID_CP;
                        if (tblcp.ContactInfoUID_Cust > 0)
                            tbl_infoUID_Cust = (int)tblcp.ContactInfoUID_Cust;
                        if (tblcp.ContactInfoFollowCust > 0)
                            tbl_infoFollowCust = (int)tblcp.ContactInfoFollowCust;
                    }
                    #endregion
                }
            }

            app_tblAddress app_address = new app_tblAddress();
            app_address = GetApp_AddressENT(app_address);
            app_tblContactInfo app_info = new app_tblContactInfo();
            app_info = getApp_infoENT(app_info);
            app_tblContactPerson app_cp = new app_tblContactPerson();
            app_cp = GetApp_cpENT(app_cp);
            app_tblCust_ContactPerson app_cust_cp = new app_tblCust_ContactPerson();
            app_cust_cp.CustomerUID = CustomerUID;
            app_cust_cp.ContactPersonUID = tbl_cpUID;
            var tempENT = app_cust_cpBLL.GetEntity(app_cust_cp,false);
            if (tempENT != null && type != "add" && ctuid > 0)
            {
                ctuid = app_cust_cp.AppCustContactUID;
                UpdateApp();
                return;
            }
            app_cust_cp = GetApp_cust_cpENT(app_cust_cp);
            app_cust_cp.CustomerUID = CustomerUID;
            if (tbl_cpUID > 0)
            {
                #region 如关联值大于0，则保存到数据库
                app_cust_cp.CustContactUID = tbl_cust_cpUID;
                app_cust_cp.ContactPersonUID = tbl_cpUID;
                app_cp.ContactPersonUID = tbl_cpUID;
                if (tbl_addressUID > 0)
                {
                    app_address.AddressUID = tbl_addressUID;
                    app_cp.AddressUID = tbl_addressUID;
                }
                if (tbl_infoUID > 0)
                {
                    app_info.ContactInfoUID = tbl_infoUID;
                    app_cp.ContactInfoUID = tbl_infoUID;
                }
                if (tbl_relatedcpUID > 0)
                    app_cp.RelatedContactPersonUID = tbl_relatedcpUID;
                if (tbl_activityUID > 0)
                    app_cp.ActivityUID = tbl_activityUID;
                if (tbl_addressUID_cp > 0)
                    app_cp.AddressUID_CP = tbl_addressUID_cp;
                if (tbl_addressUID_Cust > 0)
                    app_cp.AddressUID_Cust = tbl_addressUID_Cust;
                if (tbl_addressFollowCust > 0)
                    app_cp.AddressFollowCust = tbl_addressFollowCust;
                if (tbl_infoUID_cp > 0)
                    app_cp.ContactInfoUID_CP = tbl_infoUID_cp;
                if (tbl_infoUID_Cust > 0)
                    app_cp.ContactInfoUID_Cust = tbl_infoUID_Cust;
                if (tbl_infoFollowCust > 0)
                    app_cp.ContactInfoFollowCust = tbl_infoFollowCust;
                #endregion
            }
            List<int> ToDeletedJob = new List<int>(); //本地需要被物理删除的记录 (App表主键)
            List<app_tblCPerson_JobNature> AddToDeletedJob = new List<app_tblCPerson_JobNature>(); //本次需要被新增到app做删除标记的记录(JobNatureUID)
            List<app_tblCPerson_JobNature> AddJob = new List<app_tblCPerson_JobNature>(); //本次需要被新增的记录(JobNatureUID)
            

            List<int> ToDeletedAttribute = new List<int>(); //本地需要被物理删除的记录 (App表主键)
            List<app_tblCPerson_Attribute> AddToDeletedAttribute = new List<app_tblCPerson_Attribute>(); //本次需要被新增到app做删除标记的记录(JobNatureUID)
            List<app_tblCPerson_Attribute> AddAttribute = new List<app_tblCPerson_Attribute>(); //本次需要被新增的记录(JobNatureUID)
            

            TransactionOptions sptions = new TransactionOptions();
            sptions.Timeout = new TimeSpan(0, 5, 0);
            sptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, sptions))
            {
                int app_addressUID = app_addressBLL.InsertENT(app_address);
                int app_infoUID = app_infoBLL.InsertENT(app_info);
                app_cp.AppAddressUID = app_addressUID;
                app_cp.AppContactInfoUID = app_infoUID;
                int app_cpUID = app_cpuid = app_cpBLL.InsertENT(app_cp);
                app_cust_cp.AppContactPersonUID = app_cpUID;
                int app_cust_cpUID = app_cust_cpBLL.InsertENT(app_cust_cp);
                DetchJob(ToDeletedJob, AddToDeletedJob, AddJob);
                DetchAttr(ToDeletedAttribute, AddToDeletedAttribute, AddAttribute);
                app_jobBLL.SaveList(ToDeletedJob, AddJob, AddToDeletedJob);
                app_attrBLL.SaveList(ToDeletedAttribute, AddToDeletedAttribute, AddAttribute);
                SaveRelatedImg();
                BindImgRelated(false);
                BindCPList();
                try
                {
                    scope.Complete();
                    lblMeg.Text = ResourceManage.GetResourceString("msg200006");
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "myscript", "AverageBrand();", true);
                }
                catch (Exception er)
                {
                    lblMeg.Text = ResourceManage.GetResourceString("msg200005");
                }
            }
        }
        protected void SaveRelatedImg()
        {
            string MediaRelatedKey = hidMediaRelatedKeyUID.Value;
            string MediaKey = hidMediaKeyUID.Value;
            string MediaFrom = "";
            string MediaRelatedFrom = "";
            int MediaRelatedKeyUID = 0;
            int MediaKeyUID = 0;

            if (!string.IsNullOrEmpty(MediaRelatedKey))
            {
                string[] temp = MediaRelatedKey.Split('_');
                MediaRelatedFrom=temp[1];
                MediaRelatedKeyUID = Convert.ToInt32(temp[0]);
            }
            if (!string.IsNullOrEmpty(MediaKey))
            {
                string[] temp = MediaKey.Split('_');
                MediaFrom = temp[1];
                MediaKeyUID = Convert.ToInt32(temp[0]);
            }

            if (MediaKeyUID > 0)
            {
                if (MediaRelatedKeyUID > 0)
                {
                    if (MediaRelatedFrom == "tbl")
                    {
                        tblMediaRoom_Related tbl_related = new tblMediaRoom_Related();
                        tbl_related.MediaRoomRelatedUID = MediaRelatedKeyUID;
                        tbl_related.Deleted = 0;
                        tbl_related = tbl_relatedBLL.GetEntity(tbl_related);

                        app_tblMediaRoom_Related app_related = new app_tblMediaRoom_Related();
                        app_related.MediaRoomRelatedUID = tbl_related.MediaRoomRelatedUID;
                        if (app_cpuid > 0)
                            app_related.AppRelatedUID = app_cpuid;
                        app_related = app_relatedBLL.GetListByENT(app_related).FirstOrDefault();
                        #region tbl关联主键存在处理程序
                        if (tbl_related != null)
                        {
                            if (!string.IsNullOrEmpty(MediaFrom))
                            {
                                if (MediaFrom == "tbl")
                                {
                                    if (MediaKeyUID != tbl_related.MediaRoomUID)
                                    {
                                        if (app_related != null)
                                        {
                                            if (app_cpuid > 0)
                                                app_related.AppRelatedUID = app_cpuid;
                                            app_related.MediaRoomUID = MediaKeyUID;
                                            app_related.Category = Constant.MediaRoomCategory.contactperson_photo;
                                            app_related.IsPublish = 1;
                                            app_related.Deleted = 0;
                                            app_relatedBLL.UpdateENT(app_related);
                                        }
                                        else
                                        {
                                            app_related = new app_tblMediaRoom_Related();
                                            app_related.MediaRoomRelatedUID = MediaRelatedKeyUID;
                                            app_related.MediaRoomUID = MediaKeyUID;
                                            app_related.RelatedUID = cpuid;
                                            if (app_cpuid > 0)
                                                app_related.AppRelatedUID = app_cpuid;
                                            app_related.Category = Constant.MediaRoomCategory.contactperson_photo;
                                            app_related.IsPublish = 1;
                                            app_related.Deleted = 0;
                                            app_relatedBLL.InsertENT(app_related);
                                        }
                                    }
                                }
                                else if (MediaFrom == "app")
                                {
                                    if (app_related != null)
                                    {
                                        if (app_related.AppMediaRoomUID != MediaKeyUID)
                                        {
                                            if (app_cpuid > 0)
                                                app_related.AppRelatedUID = app_cpuid;
                                            app_related.AppMediaRoomUID = MediaKeyUID;
                                            app_relatedBLL.UpdateENT(app_related);
                                        }
                                    }
                                }
                                else
                                {
                                    if (app_related != null)
                                    {
                                        if (app_cpuid > 0)
                                            app_related.AppRelatedUID = app_cpuid;
                                        app_related.Deleted = 1;
                                        app_relatedBLL.UpdateENT(app_related);
                                    }
                                    else
                                    {
                                        app_related.MediaRoomRelatedUID = MediaRelatedKeyUID;
                                        app_related.MediaRoomUID = MediaKeyUID;
                                        app_related.RelatedUID = cpuid;
                                        if (app_cpuid > 0)
                                            app_related.AppRelatedUID = app_cpuid;
                                        app_related.Category = Constant.MediaRoomCategory.contactperson_photo;
                                        app_related.Deleted = 1;
                                        app_relatedBLL.InsertENT(app_related);
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                    else if(MediaRelatedFrom=="app")
                    {
                        app_tblMediaRoom_Related app_related = new app_tblMediaRoom_Related();
                        app_related.AppMediaRoomRelatedUID = MediaRelatedKeyUID;
                        app_related = app_relatedBLL.GetListByENT(app_related).FirstOrDefault();
                        if (app_related != null)
                        {
                            if (MediaFrom == "tbl")
                                app_related.MediaRoomUID = MediaKeyUID;
                            else
                                app_related.AppMediaRoomUID = MediaKeyUID;
                            if (cpuid > 0)
                                app_related.RelatedUID = cpuid;
                            if (app_cpuid > 0)
                                app_related.AppRelatedUID = app_cpuid;
                            app_related.Deleted = 0;
                            app_relatedBLL.UpdateENT(app_related);
                        }
                    }
                }
                else //新增
                {
                    app_tblMediaRoom_Related app_related = new app_tblMediaRoom_Related();
                    if (MediaFrom == "app")
                    {
                        app_related.AppMediaRoomUID = MediaKeyUID;
                        if (cpuid > 0)
                            app_related.RelatedUID = cpuid;
                        if (app_cpuid > 0)
                            app_related.AppRelatedUID = app_cpuid;
                        app_related.Category = Constant.MediaRoomCategory.contactperson_photo;
                        app_related.IsPublish = 1;
                        app_related.Deleted = 0;
                        app_relatedBLL.InsertENT(app_related);
                    }
                    else
                    {
                        app_related.MediaRoomUID = MediaKeyUID;
                        if (cpuid > 0)
                            app_related.RelatedUID = cpuid;
                        if (app_cpuid > 0)
                            app_related.AppRelatedUID = app_cpuid;
                        app_related.Category = Constant.MediaRoomCategory.contactperson_photo;
                        var tempList = app_relatedBLL.GetListByENT(app_related);

                        var tempENT = tempList.Where(a=>a.MediaRoomUID==MediaKeyUID).Where(a=>a.RelatedUID==app_related.RelatedUID).Where(a=>a.Deleted==1).FirstOrDefault();
                        if (tempENT != null)
                        {
                            if (tempENT.MediaRoomUID == MediaKeyUID)
                                app_relatedBLL.DeleteENT(tempENT);
                        }
                        else
                        {
                            app_related.Deleted = 0;
                            app_related.IsPublish = 1;
                            app_relatedBLL.InsertENT(app_related);
                        }
                    }
                }
            }
            else //删除、根据From判断 ;App:直接删除； tbl：新增或修改到deleted=1
            {
                if (MediaRelatedKeyUID > 0)
                {
                    tblMediaRoom_Related tbl_related = new tblMediaRoom_Related();
                    tbl_related.MediaRoomRelatedUID = MediaRelatedKeyUID;
                    tbl_related = tbl_relatedBLL.GetEntity(tbl_related);

                    app_tblMediaRoom_Related app_related = new app_tblMediaRoom_Related();
                    if (MediaRelatedFrom == "app")
                    {
                        app_related.AppMediaRoomRelatedUID = MediaRelatedKeyUID;
                    }
                    else
                    {
                        app_related.MediaRoomRelatedUID = MediaRelatedKeyUID;
                    }
                    app_related = app_relatedBLL.GetListByENT(app_related).FirstOrDefault();
                    if (MediaRelatedFrom == "tbl")
                    {
                        if (app_related != null)
                        {
                            app_related.Deleted = 1;
                            app_relatedBLL.UpdateENT(app_related);
                        }
                        else
                        {
                            app_related = new app_tblMediaRoom_Related();
                            if (app_cpuid > 0)
                                app_related.AppRelatedUID = app_cpuid;
                            if (tbl_related != null)
                            {
                                app_related.MediaRoomRelatedUID = tbl_related.MediaRoomRelatedUID;
                                app_related.MediaRoomUID = tbl_related.MediaRoomUID;
                                app_related.RelatedUID = tbl_related.RelatedUID;
                            }
                            app_related.Deleted = 1;
                            app_related.Category = Constant.MediaRoomCategory.contactperson_photo;
                            app_related.IsPublish = 1;
                            app_relatedBLL.InsertENT(app_related);
                        }
                    }
                    else
                    {
                        if (app_related != null)
                        {
                            if (app_related.MediaRoomRelatedUID > 0)
                            {
                                app_related.Deleted = 1;
                                app_relatedBLL.UpdateENT(app_related);
                            }
                            else
                            {
                                app_related.Deleted = 0;
                                app_related.MediaRoomUID = MediaKeyUID;
                                app_relatedBLL.DeleteENT(app_related);
                            }
                        }
                    }
                }
                //app_related.MediaRoomRelatedUID = MediaRelatedKeyUID;
                //app_related.MediaRoomUID = MediaKeyUID;
                //app_related.RelatedUID = cpuid;
                //if (app_cpuid > 0)
                //    app_related.AppRelatedUID = app_cpuid;
                //app_related.Category = Constant.MediaRoomCategory.contactperson_photo;
                //app_related.Deleted = 0;
                //app_relatedBLL.InsertENT(app_related);
            }
        }
        protected void lkbtnSave_Click(object sender, EventArgs e)
        {
            //customerENT = GetCustomerFromSession();
            //if (customerENT != null)
            //{
            //    CustomerUID = customerENT.CustomerUID;
            //    string strctcuid = Request.QueryString[Constant.UrlParamName.custToContactPersonUID];
            //    type = Request.QueryString["type"];
            //    if (!string.IsNullOrEmpty(strctcuid) && int.TryParse(strctcuid, out ctuid) && ctuid > 0)
            //    {
            //        Update();
            //    }
            //    else
            //    {
            //        Add();
            //        ScriptManager.RegisterStartupScript(this, this.GetType(), "cleartxt", "ClearText();", true);
            //    }
                WriteToLog();
            //}
        }
        #region 获取文本值
        private app_tblCust_ContactPerson GetApp_cust_cpENT(app_tblCust_ContactPerson ent)
        {
            ent.LastModifyDate = DateTime.Now;
            ent.Position_Chi = txtPosition_Chi.Text;
            ent.Position_Eng = txtPosition_Eng.Text;
            return ent;
        }
        private app_tblContactPerson GetApp_cpENT(app_tblContactPerson cpENT)
        {
            cpENT.SalutationUID = Convert.ToInt32(ddlSalutation.SelectedValue);
            cpENT.ChristianName = txtChristianName.Text;
            cpENT.FirstName_Eng = txtFirstName_Eng.Text;
            cpENT.LastName_Eng = txtLastName_Eng.Text;
            cpENT.OtherTitle_Eng = txtOtherTitle_Eng.Text;
            cpENT.FirstName_Chi = txtFirstName_Chi.Text;
            cpENT.LastName_Chi = txtLastName_Chi.Text;
            cpENT.OtherTitle_Chi = txtOtherTitle_Chi.Text;
            cpENT.CreationDate = DateTime.Now;
            cpENT.LastModifyDate = DateTime.Now;
            cpENT.LastModifyBy = Common.GetManageName();
            cpENT.Relationship = txtRelationship.Text;
            cpENT.Remark = txtRemark.Text;
            cpENT.HKID_PassportID = hidTxtHKID.Value;
            return cpENT;
        }
        private app_tblContactInfo getApp_infoENT(app_tblContactInfo infoENT)
        {
            #region Email
            infoENT.Email1 = TextBoxEmail1.Text;
            infoENT.Email1_IsSubscribe = CheckBoxES1.Checked ? 1 : 0;
            infoENT.Email1_Type = Convert.ToInt32(DropDownListEm1.SelectedValue);
            infoENT.Email1_HardBounce = CheckBoxHB1.Checked ? 1 : 0;
            infoENT.Email2 = TextBoxEmail2.Text;
            infoENT.Email2_IsSubscribe = CheckBoxES2.Checked ? 1 : 0;
            infoENT.Email2_Type = Convert.ToInt32(DropDownListEm2.SelectedValue);
            infoENT.Email2_HardBounce = CheckBoxHB2.Checked ? 1 : 0;
            infoENT.Email3 = TextBoxEmail3.Text;
            infoENT.Email3_IsSubscribe = CheckBoxES3.Checked ? 1 : 0;
            infoENT.Email3_Type = Convert.ToInt32(DropDownListEm3.SelectedValue);
            infoENT.Email3_HardBounce = CheckBoxHB3.Checked ? 1 : 0;
            infoENT.Email4 = TextBoxEmail4.Text;
            infoENT.Email4_IsSubscribe = CheckBoxES4.Checked ? 1 : 0;
            infoENT.Email4_Type = Convert.ToInt32(DropDownListEm4.SelectedValue);
            infoENT.Email4_HardBounce = CheckBoxHB4.Checked ? 1 : 0;
            infoENT.Email5 = TextBoxEmail5.Text;
            infoENT.Email5_IsSubscribe = CheckBoxES5.Checked ? 1 : 0;
            infoENT.Email5_Type = Convert.ToInt32(DropDownListEm5.SelectedValue);
            infoENT.Email5_HardBounce = CheckBoxHB5.Checked ? 1 : 0;
            #endregion
            #region Fax
            infoENT.Fax1 = TextBoxF1.Text;
            infoENT.Fax1_AreaCode = TextBoxFA1.Text;
            infoENT.Fax1_CountryCode = TextBoxFC1.Text;
            infoENT.Fax1_IsDNC = CheckBoxFD1.Checked ? 1 : 0;
            infoENT.Fax1_IsSubscribe = CheckBoxFS1.Checked ? 1 : 0;
            infoENT.Fax1_Type = Convert.ToInt32(DropDownListFax1.SelectedValue);

            infoENT.Fax2 = TextBoxF2.Text;
            infoENT.Fax2_AreaCode = TextBoxFA2.Text;
            infoENT.Fax2_CountryCode = TextBoxFC2.Text;
            infoENT.Fax2_IsDNC = CheckBoxFD2.Checked ? 1 : 0;
            infoENT.Fax2_IsSubscribe = CheckBoxFS2.Checked ? 1 : 0;
            infoENT.Fax2_Type = Convert.ToInt32(DropDownListFax2.SelectedValue);

            infoENT.Fax3 = TextBoxF3.Text;
            infoENT.Fax3_AreaCode = TextBoxFA3.Text;
            infoENT.Fax3_CountryCode = TextBoxFC3.Text;
            infoENT.Fax3_IsDNC = CheckBoxFD3.Checked ? 1 : 0;
            infoENT.Fax3_IsSubscribe = CheckBoxFS3.Checked ? 1 : 0;
            infoENT.Fax3_Type = Convert.ToInt32(DropDownListFax3.SelectedValue);

            infoENT.Fax4 = TextBoxF4.Text;
            infoENT.Fax4_AreaCode = TextBoxFA4.Text;
            infoENT.Fax4_CountryCode = TextBoxFC4.Text;
            infoENT.Fax4_IsDNC = CheckBoxFD4.Checked ? 1 : 0;
            infoENT.Fax4_IsSubscribe = CheckBoxFS4.Checked ? 1 : 0;
            infoENT.Fax4_Type = Convert.ToInt32(DropDownListFax4.SelectedValue);

            infoENT.Fax5 = TextBoxF5.Text;
            infoENT.Fax5_AreaCode = TextBoxFA5.Text;
            infoENT.Fax5_CountryCode = TextBoxFC5.Text;
            infoENT.Fax5_IsDNC = CheckBoxFD5.Checked ? 1 : 0;
            infoENT.Fax5_IsSubscribe = CheckBoxFS5.Checked ? 1 : 0;
            infoENT.Fax5_Type = Convert.ToInt32(DropDownListFax5.SelectedValue);
            #endregion
            #region Tel
            infoENT.Tel1 = TextBoxT1.Text;
            infoENT.Tel1_AreaCode = TextBoxTA1.Text;
            infoENT.Tel1_CountryCode = TextBoxTC1.Text;
            infoENT.Tel1_IsDNC = CheckBoxTD1.Checked ? 1 : 0;
            infoENT.Tel1_IsSubscribe = CheckBoxTS1.Checked ? 1 : 0;
            infoENT.Tel1_Type = Convert.ToInt32(DropDownListTel1.SelectedValue);

            infoENT.Tel2 = TextBoxT2.Text;
            infoENT.Tel2_AreaCode = TextBoxTA2.Text;
            infoENT.Tel2_CountryCode = TextBoxTC2.Text;
            infoENT.Tel2_IsDNC = CheckBoxTD2.Checked ? 1 : 0;
            infoENT.Tel2_IsSubscribe = CheckBoxTS2.Checked ? 1 : 0;
            infoENT.Tel2_Type = Convert.ToInt32(DropDownListTel2.SelectedValue);

            infoENT.Tel3 = TextBoxT3.Text;
            infoENT.Tel3_AreaCode = TextBoxTA3.Text;
            infoENT.Tel3_CountryCode = TextBoxTC3.Text;
            infoENT.Tel3_IsDNC = CheckBoxTD3.Checked ? 1 : 0;
            infoENT.Tel3_IsSubscribe = CheckBoxTS3.Checked ? 1 : 0;
            infoENT.Tel3_Type = Convert.ToInt32(DropDownListTel3.SelectedValue);

            infoENT.Tel4 = TextBoxT4.Text;
            infoENT.Tel4_AreaCode = TextBoxTA4.Text;
            infoENT.Tel4_CountryCode = TextBoxTC4.Text;
            infoENT.Tel4_IsDNC = CheckBoxTD4.Checked ? 1 : 0;
            infoENT.Tel4_IsSubscribe = CheckBoxTS4.Checked ? 1 : 0;
            infoENT.Tel4_Type = Convert.ToInt32(DropDownListTel4.SelectedValue);

            infoENT.Tel5 = TextBoxT5.Text;
            infoENT.Tel5_AreaCode = TextBoxTA5.Text;
            infoENT.Tel5_CountryCode = TextBoxTC5.Text;
            infoENT.Tel5_IsDNC = CheckBoxTD5.Checked ? 1 : 0;
            infoENT.Tel5_IsSubscribe = CheckBoxTS5.Checked ? 1 : 0;
            infoENT.Tel5_Type = Convert.ToInt32(DropDownListTel5.SelectedValue);
            #endregion

            #region WebSite
            infoENT.Website1 = TextBoxWeb1.Text;
            infoENT.Website1_Type = Convert.ToInt32(DropDownListW1.SelectedValue);
            infoENT.Website2 = TextBoxWeb2.Text;
            infoENT.Website2_Type = Convert.ToInt32(DropDownListW2.SelectedValue);
            #endregion
            return infoENT;
        }
        private app_tblAddress GetApp_AddressENT(app_tblAddress addressENT)
        {
            addressENT.Area_Chi = txtArea_Chi.Text;
            addressENT.Area_Eng = txtArea_Eng.Text;
            addressENT.City_Chi = txtCity_Chi.Text;
            addressENT.City_Eng = txtCity_Eng.Text;
            addressENT.CountryUID = Convert.ToInt32(ddlCountry.SelectedValue);
            addressENT.CreationDate = DateTime.Now;
            addressENT.District_Chi = txtDistrict_Chi.Text;
            addressENT.District_Eng = txtDistrict_Eng.Text;
            addressENT.Flat_Bldg_Chi = txtFlat_Chi.Text;
            addressENT.Flat_Bldg_Eng = txtFlat_Eng.Text;
            addressENT.LastModifyBy = Common.GetManageName();
            addressENT.LastModifyDate = DateTime.Now;
            addressENT.PostalCode = txtPostCode.Text;
            addressENT.Province_Chi = txtProvince_Chi.Text;
            addressENT.Province_Eng = txtProvince_Eng.Text;
            addressENT.Street_Rd_Chi = txtStreet_Chi.Text;
            addressENT.Street_Rd_Eng = txtStreet_Eng.Text;
            addressENT.Department_Chi = hidtxtDepartment_Chi.Value;
            addressENT.Department_Eng = hidtxtDepartment_Eng.Value;
            mtblCountry countryENT = new mtblCountry();
            countryENT = addressENT.mtblCountry;
            StringBuilder strBuilderEng = new StringBuilder();
            strBuilderEng.Append(txtFlat_Eng.Text).Append(txtStreet_Eng.Text).Append(txtArea_Eng.Text).Append(txtDistrict_Eng.Text).Append(txtCity_Eng.Text).Append(txtProvince_Eng.Text);
            StringBuilder strBuilderChi = new StringBuilder();
            if (countryENT != null)
            {
                strBuilderEng.Append(countryENT.Country_Eng);

                if (countryENT.AreaUID > 0)
                {
                    mtblArea areaENT = new mtblArea();
                    areaENT.AreaUID = (int)countryENT.AreaUID;
                    mtblAreaBLL bll = new mtblAreaBLL();
                    areaENT = bll.GetEntity(areaENT);
                    if (areaENT != null)
                    {
                        strBuilderEng.Append(areaENT.Area_Eng);
                        strBuilderChi.Append(areaENT.Area_Chi);
                    }
                }
                strBuilderChi.Append(countryENT.Country_Chi);
            }
            strBuilderChi.Append(txtProvince_Chi.Text).Append(txtCity_Chi.Text).Append(txtDistrict_Chi.Text).Append(txtArea_Chi.Text).Append(txtStreet_Chi.Text).Append(txtFlat_Chi.Text);
            addressENT.FullAdd_Chi = strBuilderChi.ToString();
            addressENT.FullAdd_Eng = strBuilderEng.ToString();
            return addressENT;
        }
        private void DetchJob(List<int> ToDeleted, List<app_tblCPerson_JobNature> AddToDeleted, List<app_tblCPerson_JobNature> Add)
        {
            var CheckedList = GetCheckedJob();
            var tbl_List = GetTBLJobNature();
            var app_List = GetAppJobNature();
            #region tbl_list处理
            if (tbl_List != null && tbl_List.Count > 0)
            {
                foreach (var temp in tbl_List)
                {
                    if (!CheckedList.Contains((int)temp.JobNatureUID)) //需要被添加到APP做删除标记的TBL记录
                    {
                        if (app_List != null)
                        {
                            if (app_List.Where(a => a.JobNatureUID == (int)temp.JobNatureUID).Where(a => a.Deleted == 1).ToList().Count == 0)
                            {
                                app_tblCPerson_JobNature ent = new app_tblCPerson_JobNature();
                                if (app_cpuid > 0)
                                    ent.AppContactPersonUID = app_cpuid;
                                if (cpuid > 0)
                                    ent.ContactPersonUID = cpuid;
                                ent.CPerson_JobNatureUID = temp.CPerson_JobNatureUID;
                                ent.Deleted = 1;
                                ent.JobNatureUID = (int)temp.JobNatureUID;
                                ent.Remarks = ent.Remarks;
                                AddToDeleted.Add(ent);
                            }
                        }
                        else {
                            app_tblCPerson_JobNature ent = new app_tblCPerson_JobNature();
                            if (app_cpuid > 0)
                                ent.AppContactPersonUID = app_cpuid;
                            if (cpuid > 0)
                                ent.ContactPersonUID = cpuid;
                            ent.CPerson_JobNatureUID = temp.CPerson_JobNatureUID;
                            ent.Deleted = 1;
                            ent.JobNatureUID = (int)temp.JobNatureUID;
                            ent.Remarks = ent.Remarks;
                            AddToDeleted.Add(ent);
                        }
                    }
                }
            }
            #endregion
            #region app_list处理
            if (app_List != null && app_List.Where(a => a.Deleted == 0).ToList().Count > 0) //存在新增App数据，被取消
            {
                foreach (var temp in app_List.Where(a => a.Deleted == 0).ToList())
                {
                    if (!CheckedList.Contains((int)temp.JobNatureUID))
                    {
                        ToDeleted.Add(temp.AppCPerson_JobNatureUID);
                    }
                }
            }
            #endregion
            # region checkList处理
            if (CheckedList.Count > 0)
            {
                foreach (var temp in CheckedList)
                {
                    bool NeedToAdd = true;
                    if (app_List != null && app_List.Count > 0)
                    {
                        var tempApp = app_List.Where(a => a.JobNatureUID == temp).Where(a => a.Deleted == 1).FirstOrDefault();
                        if (tempApp != null)
                            ToDeleted.Add(tempApp.AppCPerson_JobNatureUID);
                        else if (app_List.Where(a => a.JobNatureUID == temp).Where(a => a.Deleted == 0).FirstOrDefault() != null)
                        {
                            NeedToAdd = false;
                        }
                    }
                    if(tbl_List != null && tbl_List.Count > 0)
                    {
                        var tempTbl = tbl_List.Where(a => a.JobNatureUID == temp).Where(a=>a.Deleted==0).FirstOrDefault();
                        if (tempTbl != null)
                        {
                            NeedToAdd = false;
                        }
                    }
                    if (NeedToAdd)
                    {
                        app_tblCPerson_JobNature ent = new app_tblCPerson_JobNature();
                        if(app_cpuid > 0)
                            ent.AppContactPersonUID = app_cpuid;
                        if(cpuid > 0)
                            ent.ContactPersonUID = cpuid;
                        ent.Deleted = 0;
                        ent.JobNatureUID = temp;
                        ent.Remarks = ent.Remarks;
                        Add.Add(ent);
                    }
                }
            }
            #endregion
        }
        private void DetchAttr(List<int> ToDeleted, List<app_tblCPerson_Attribute> AddToDeleted, List<app_tblCPerson_Attribute> Add)
        {
            var CheckedList = GetCheckedAttribute();
            var tbl_List= GetTBLAttr();
            var app_List = GetAppAttr();
            #region tbl_list处理
            if (tbl_List != null && tbl_List.Count > 0)
            {
                foreach (var temp in tbl_List)
                {
                    if (!CheckedList.Contains((int)temp.AttributeUID)) //需要被添加到APP做删除标记的TBL记录
                    {
                        if (app_List!=null && app_List.Where(a => a.AttributeUID == (int)temp.AttributeUID).Where(a => a.Deleted == 1).ToList().Count == 0)
                        {
                            app_tblCPerson_Attribute ent = new app_tblCPerson_Attribute();
                            if (app_cpuid > 0)
                                ent.AppContactPersonUID = app_cpuid;
                            if (cpuid > 0)
                                ent.ContactPersonUID = cpuid;
                            ent.CPerson_AttributeUID = temp.CPerson_AttributeUID;
                            ent.Deleted = 1;
                            ent.AttributeUID = (int)temp.AttributeUID;
                            AddToDeleted.Add(ent);
                        }
                        else
                        {
                            app_tblCPerson_Attribute ent = new app_tblCPerson_Attribute();
                            if (app_cpuid > 0)
                                ent.AppContactPersonUID = app_cpuid;
                            if (cpuid > 0)
                                ent.ContactPersonUID = cpuid;
                            ent.CPerson_AttributeUID = temp.CPerson_AttributeUID;
                            ent.Deleted = 1;
                            ent.AttributeUID = (int)temp.AttributeUID;
                            AddToDeleted.Add(ent);
                        }
                    }
                }
            }
            #endregion
            #region app_list处理
            if (app_List != null && app_List.Where(a => a.Deleted == 0).ToList().Count > 0) //存在新增App数据，被取消
            {
                foreach (var temp in app_List.Where(a => a.Deleted == 0).ToList())
                {
                    if (!CheckedList.Contains((int)temp.AttributeUID))
                    {
                        ToDeleted.Add(temp.AppCPerson_AttributeUID);
                    }
                }
            }
            #endregion
            # region checkList处理
            if (CheckedList.Count > 0)
            {
                foreach (var temp in CheckedList)
                {
                    bool NeedToAdd = true;
                    if (app_List != null && app_List.Count > 0)
                    {
                        var tempApp = app_List.Where(a => a.AttributeUID == temp).Where(a => a.Deleted == 1).FirstOrDefault();
                        if (tempApp != null)
                            ToDeleted.Add(tempApp.AppCPerson_AttributeUID);
                        else if (app_List.Where(a => a.AttributeUID == temp).Where(a => a.Deleted == 0).FirstOrDefault() != null)
                        {
                            NeedToAdd = false;
                        }
                    }
                    if (tbl_List != null && tbl_List.Count > 0)
                    {
                        var tempTbl = tbl_List.Where(a => a.AttributeUID == temp).Where(a => a.Deleted == 0).FirstOrDefault();
                        if (tempTbl != null)
                        {
                            NeedToAdd = false;
                        }
                    }
                    if(NeedToAdd)
                    {
                        app_tblCPerson_Attribute ent = new app_tblCPerson_Attribute();
                        if (app_cpuid > 0)
                            ent.AppContactPersonUID = app_cpuid;
                        if (cpuid > 0)
                            ent.ContactPersonUID = cpuid;
                        ent.Deleted = 0;
                        ent.AttributeUID = temp;
                        Add.Add(ent);
                    }
                }
            }
            #endregion
        }
        
        private  List<int> GetCheckedJob()
        { return GetList(hidJobHasChecked); }
        private List<int> GetCheckedAttribute()
        { return GetList(hidAttriHasChecked); }        
        protected List<int> GetList(HiddenField name)
        {
            List<int> ret = new List<int>();
            if (!string.IsNullOrEmpty(name.Value))
            {
                ret = name.Value.StringToList();
            }
            return ret;
        }
        protected List<tblCPerson_JobNature> GetTBLJobNature()
        {
            if (cpuid > 0)
            {
                tblCPerson_JobNature jobnature = new tblCPerson_JobNature();
                jobnature.ContactPersonUID = cpuid;
                jobnature.Deleted = 0;
                return jobBLL.GetListByENT(jobnature);
            }
            return null;
        }
        protected List<app_tblCPerson_JobNature> GetAppJobNature()
        {
            if (app_cpuid > 0)
            {
                app_tblCPerson_JobNature jobnature = new app_tblCPerson_JobNature();
                jobnature.AppContactPersonUID = app_cpuid;
                return app_jobBLL.GetListByENT(jobnature, false);
            }
            return null;
        }
        protected List<tblCPerson_Attribute> GetTBLAttr()
        {
            if (cpuid > 0)
            {
                tblCPerson_Attribute attr = new tblCPerson_Attribute();
                attr.ContactPersonUID = cpuid;
                attr.Deleted = 0;
                return attrBLL.GetListByENT(attr);
            }
            return null;
        }
        protected List<app_tblCPerson_Attribute> GetAppAttr()
        {
            if (app_cpuid > 0)
            {
                app_tblCPerson_Attribute attr = new app_tblCPerson_Attribute();
                attr.ContactPersonUID = cpuid;
                attr.Deleted = 0;
                return app_attrBLL.GetListByENT(attr, false);
            }
            return null;
        }
        
        #endregion

        #region nicky Log
        static app_tblContactPerson staticCPerson;
        static app_tblContactInfo staticContactInfo;
        static app_tblAddress staticAddress;
        static string ActivityTypeUID = string.Empty;
        static List<CommitteeMemberList> listCPersonCommMembe;
        static string MedioVal;

        /// <summary>
        /// 将初始化的数据写入静态变量
        /// </summary>
        protected void BindStaticInfo()
        {
            ActivityTypeUID = string.Empty;
            staticCPerson = new app_tblContactPerson();
            staticContactInfo = new app_tblContactInfo();
            staticAddress = new app_tblAddress();
            staticCPerson = GetApp_cpENT(staticCPerson);
            staticContactInfo = getApp_infoENT(staticContactInfo);
            staticAddress = GetApp_AddressENT(staticAddress);
            listCPersonCommMembe = BindCommitteeMember();
            MedioVal = hidMediaKeyUID.Value; 
        }
        protected string UploadMide(string info)
        {
            string Media = hidMediaKeyUID.Value;
            if (MedioVal.Trim() != Media.Trim())
            {
                info = "ContactPerson Image have change";
            }
            return info;
        }

        protected string CPersonCommMembe(string info)
        {
            mtblCommPosition entCommPostion=new mtblCommPosition();
            mtblCommType entCommType=new mtblCommType();
            List<mtblCommPosition> listsCommPostion = mtblCommPositionBLL.GetInstance().GetListByENT(entCommPostion);
            List<mtblCommType> listsCommType = mtblCommTypeBLL.GetInstance().GetListByENT(entCommType);
            List<CommitteeMemberList> list = new List<CommitteeMemberList>();
            list = BindCommitteeMember();
            
            if (listCPersonCommMembe.Count() != list.Count())
            {
                info = "Committee Member :";
                for (int i = 0; i < listCPersonCommMembe.Count(); i++)
                {
                    var m = list.Select(a => a.KeyUID).Contains(listCPersonCommMembe[i].KeyUID);
                    if (m == false)
                    {
                        info += "Year / Term:" + listCPersonCommMembe[i].Year + ",Date:" + listCPersonCommMembe[i].StartDate + " to " + listCPersonCommMembe[i].EndDate + "  Committee Type:" + listsCommType.Where(a => a.CommTypeUID == listCPersonCommMembe[i].mtblType.CommTypeUID).First().CommType_Eng + ",Committee Position:" + listsCommPostion.Where(a => a.CommPositionUID == listCPersonCommMembe[i].mtblPosition.CommPositionUID).First().CommPosition_Eng + "have been Delete";
                    }
                }

                for (int i = 0; i < list.Count(); i++)
                {
                    var m = listCPersonCommMembe.Select(a => a.KeyUID).Contains(list[i].KeyUID);
                    if (m == false)
                    {
                        info += " a information with Year / Term:" + list[i].Year + ",Date:" + list[i].StartDate + " to " + list[i].EndDate + "  Committee Type:" + listsCommType.Where(a => a.CommTypeUID == list[i].mtblType.CommTypeUID).First().CommType_Eng + ",Committee Position:" + listsCommPostion.Where(a => a.CommPositionUID == list[i].mtblPosition.CommPositionUID).First().CommPosition_Eng + "have been add ";
                    }
                }
            }
            return info;
        }
        protected string CPerson(string info)
        {
            if (staticCPerson.SalutationUID != Convert.ToInt32(ddlSalutation.SelectedValue)) { info += " Salutation change to" + ddlSalutation.DataTextField; }
            if (Val(staticCPerson.ChristianName) != txtChristianName.Text) { info += " ChristianName change to" + txtChristianName.Text; }
            if (Val(staticCPerson.FirstName_Eng) != txtFirstName_Eng.Text) { info += " First Name change to" + txtFirstName_Eng.Text; }
            if (Val(staticCPerson.LastName_Eng) != txtLastName_Eng.Text) { info += " Last Name change to" + txtLastName_Eng.Text; }
            if (Val(staticCPerson.OtherTitle_Eng) != txtOtherTitle_Eng.Text) { info += " Other Title change to" + txtOtherTitle_Eng.Text; }
            if (Val(staticCPerson.FirstName_Chi) != txtFirstName_Chi.Text) { info += "姓 change to" + txtFirstName_Chi.Text; }
            if (Val(staticCPerson.LastName_Chi) != txtLastName_Chi.Text) { info += "名 change to" + txtLastName_Chi.Text; }
            if (Val(staticCPerson.OtherTitle_Chi) != txtOtherTitle_Chi.Text) { info += "	名銜  change to" + txtOtherTitle_Chi.Text; }
            if (Val(staticCPerson.HKID_PassportID) != hidTxtHKID.Value) { info += " HKID change to" + hidTxtHKID.Value; }
            return info;
        }
        protected string contactInfo(string info)
        {
            if (staticContactInfo != null)
            {
                app_tblContactInfo infoENT = new app_tblContactInfo();
                infoENT = getApp_infoENT(infoENT);

                if (Val(staticContactInfo.Tel1) != Val(infoENT.Tel1) | Val(staticContactInfo.Tel2) != Val(infoENT.Tel2) | Val(staticContactInfo.Tel3) != Val(infoENT.Tel3) | Val(staticContactInfo.Tel4) != Val(infoENT.Tel4) | Val(staticContactInfo.Tel5) != Val(infoENT.Tel5))
                {
                    info += "Tel have change; ";
                }
                if (staticContactInfo.Tel1_IsDNC != infoENT.Tel1_IsDNC | staticContactInfo.Tel2_IsDNC != infoENT.Tel2_IsDNC | staticContactInfo.Tel3_IsDNC != infoENT.Tel3_IsDNC | staticContactInfo.Tel4_IsDNC != infoENT.Tel4_IsDNC | staticContactInfo.Tel5_IsDNC != infoENT.Tel5_IsDNC)
                {
                    info += "Tel DNC have change; ";
                }
                if (staticContactInfo.Tel1_IsSubscribe != infoENT.Tel1_IsSubscribe | staticContactInfo.Tel2_IsSubscribe != infoENT.Tel2_IsSubscribe | staticContactInfo.Tel3_IsSubscribe != infoENT.Tel3_IsSubscribe | staticContactInfo.Tel4_IsSubscribe != infoENT.Tel4_IsSubscribe | staticContactInfo.Tel5_IsSubscribe != infoENT.Tel5_IsSubscribe)
                {
                    info += "Tel Subscrib have change; ";
                }
                if (Val(staticContactInfo.Fax1) != Val(infoENT.Fax1) | Val(staticContactInfo.Fax2) != Val(infoENT.Fax2) | Val(staticContactInfo.Fax3) != Val(infoENT.Fax3) | Val(staticContactInfo.Fax4) != Val(infoENT.Fax4) | Val(staticContactInfo.Fax5) != Val(infoENT.Fax5))
                {
                    info += "Fax have change";
                }
                if (staticContactInfo.Fax1_IsDNC != infoENT.Fax1_IsDNC | staticContactInfo.Fax2_IsDNC != infoENT.Fax2_IsDNC | staticContactInfo.Fax3_IsDNC != infoENT.Fax3_IsDNC | staticContactInfo.Fax4_IsDNC != infoENT.Fax4_IsDNC | staticContactInfo.Fax5_IsDNC != infoENT.Fax5_IsDNC)
                {
                    info += "Fax DNC chave change";
                }
                if (staticContactInfo.Fax1_IsSubscribe != infoENT.Fax1_IsSubscribe | staticContactInfo.Fax2_IsSubscribe != infoENT.Fax2_IsSubscribe | staticContactInfo.Fax3_IsSubscribe != infoENT.Fax3_IsSubscribe | staticContactInfo.Fax4_IsSubscribe != infoENT.Fax4_IsSubscribe | staticContactInfo.Fax5_IsSubscribe != infoENT.Fax5_IsSubscribe)
                {
                    info += "Fax Subscribe have change";
                }
                if (Val(staticContactInfo.Email1) != Val(infoENT.Email1) | Val(staticContactInfo.Email2) != Val(infoENT.Email2) | Val(staticContactInfo.Email3) != Val(infoENT.Email3) | Val(staticContactInfo.Email4) != Val(infoENT.Email4) | Val(staticContactInfo.Email5) != Val(infoENT.Email5))
                {
                    info += "Email have change";
                }
                if (staticContactInfo.Email1_IsSubscribe != infoENT.Email1_IsSubscribe | staticContactInfo.Email2_IsSubscribe != infoENT.Email2_IsSubscribe | staticContactInfo.Email3_IsSubscribe != infoENT.Email3_IsSubscribe | staticContactInfo.Email4_IsSubscribe != infoENT.Email4_IsSubscribe | staticContactInfo.Email5_IsSubscribe != infoENT.Email5_IsSubscribe)
                {
                    info += "Email Subscribe have change";
                }
                if (staticContactInfo.Email1_HardBounce != infoENT.Email1_HardBounce | staticContactInfo.Email2_HardBounce != infoENT.Email2_HardBounce | staticContactInfo.Email3_HardBounce != infoENT.Email3_HardBounce | staticContactInfo.Email4_HardBounce != infoENT.Email4_HardBounce | staticContactInfo.Email5_HardBounce != infoENT.Email5_HardBounce)
                {
                    info += "Email Subscribe have change";
                }
                if (staticContactInfo.Website1 != infoENT.Website1 | staticContactInfo.Website2 != infoENT.Website2 | staticContactInfo.Website3 != infoENT.Website3 | staticContactInfo.Website4 != infoENT.Website4 | staticContactInfo.Website5 != infoENT.Website5)
                {
                    info += "Website have change";
                }
                if (staticContactInfo.Website1_Type != infoENT.Website1_Type | staticContactInfo.Website2_Type != infoENT.Website2_Type)
                {
                    info += "Website type have change";
                }
            }
            return info;
        }
        protected string AddressInfo(string info)
        {
            if (Val(staticAddress.Area_Chi) != txtArea_Chi.Text) { info += ", 地區 change to " + txtArea_Chi.Text; }
            if (Val(staticAddress.Area_Eng) != txtArea_Eng.Text) { info += " , Area change to " + txtArea_Eng.Text; }
            if (Val(staticAddress.City_Chi) != txtCity_Chi.Text) { info += " , 城市 change to " + txtCity_Chi.Text; }
            if (Val(staticAddress.City_Eng) != txtCity_Eng.Text) { info += " , City change to " + txtCity_Eng.Text; }
            if (Val(staticAddress.District_Chi) != txtDistrict_Chi.Text) { info += " , 區域 change to " + txtDistrict_Chi.Text; }
            if (Val(staticAddress.District_Eng) != txtDistrict_Eng.Text) { info += " , District change to " + txtDistrict_Eng.Text; }
            if (Val(staticAddress.Flat_Bldg_Eng) != txtFlat_Eng.Text) { info += " , 室, 樓層, 大廈 change to " + txtFlat_Eng.Text; }
            if (Val(staticAddress.Flat_Bldg_Chi) != txtFlat_Chi.Text) { info += " , Flat_Bldg change to " + txtFlat_Chi.Text; }
            if (Val(staticAddress.Province_Chi) != txtProvince_Chi.Text) { info += " , 州, 省 change to " + txtProvince_Chi.Text; }
            if (Val(staticAddress.Province_Eng) != txtProvince_Eng.Text) { info += " , Province change to " + txtProvince_Eng.Text; }
            if (Val(staticAddress.Street_Rd_Chi) != txtStreet_Chi.Text) { info += " , 街道 change to " + txtStreet_Chi.Text; }
            if (Val(staticAddress.Street_Rd_Eng) != txtStreet_Eng.Text) { info += " , Street_Rd change to" + txtStreet_Eng.Text; }
            if (Val(staticAddress.PostalCode) != txtPostCode.Text) { info += " , PostalCode change to " + txtPostCode.Text; }
            if (Val(staticAddress.Department_Eng) != ddlArea.SelectedItem.Text) { info += " , Continent change to " + ddlArea.SelectedItem.Text; }
            if (Val(staticAddress.Department_Chi) != ddlCountry.SelectedItem.Text) { info += " , Country change to" + ddlCountry.SelectedItem.Text; }
            return info;
        }
        protected string JobNatureInfo(string info)
        {
            List<int> addJob = new List<int>();
            List<int> delJob = new List<int>();

            mtblJobNature entJob = new mtblJobNature();
            List<mtblJobNature> A_JobList = mtblJobNatureBLL.GetInstance().GetListByENT(entJob);

            addJob = GetChangeList(GetList(hidJobHasChecked),GetList(hidJobNatureUID));
            delJob = GetChangeList(GetList(hidJobNatureUID), GetList(hidJobHasChecked));
            if (addJob.Count() > 0)
            {
                info += "JobNature Add ";
                for (int i = 0; i < addJob.Count - 1; i++)
                {
                    var result = A_JobList.Where(a => a.JobNatureUID == Convert.ToInt32(addJob[i])).Select(p => p).ToList();
                    foreach (mtblJobNature item in result)
                    {
                        info += item.JobNature_Eng + ",";
                    }
                }
            }
            if (delJob.Count() > 0)
            {
                info += "JobNature delete ";
                for (int i = 0; i < delJob.Count - 1; i++)
                {
                    var result = A_JobList.Where(a => a.JobNatureUID == Convert.ToInt32(delJob[i])).Select(p => p).ToList();
                    foreach (mtblJobNature item in result)
                    {
                        info += item.JobNature_Eng + ",";
                    }
                }
            }
            return info;
        }
        protected string JobAttributeInfo(string info)
        {
            List<int> addAttr = new List<int>();
            List<int> delAttr = new List<int>();

            mtblAttribute entAttr = new mtblAttribute();
            List<mtblAttribute> A_AttrList = mtblAttributeBLL.GetInstance().GetListByENT(entAttr);

            addAttr = GetChangeList(GetList(hidAttriHasChecked), GetList(hidAttributeUID));
            delAttr = GetChangeList(GetList(hidAttributeUID), GetList(hidAttriHasChecked));
            if (addAttr.Count() > 0)
            {
                info += "Attribute Add ";
                for (int i = 0; i < addAttr.Count - 1; i++)
                {
                    var result = A_AttrList.Where(a => a.AttributeUID == Convert.ToInt32(addAttr[i])).Select(p => p).ToList();
                    foreach (mtblAttribute item in result)
                    {
                        info += item.Attribute_Eng + ",";
                    }
                }
            }
            if (delAttr.Count() > 0)
            {
                info += "Attribute delete ";
                for (int i = 0; i < delAttr.Count - 1; i++)
                {
                    var result = A_AttrList.Where(a => a.AttributeUID == Convert.ToInt32(delAttr[i])).Select(p => p).ToList();
                    foreach (mtblAttribute item in result)
                    {
                        info += item.Attribute_Eng + ",";
                    }
                }
            }
            return info;
        }
        protected void WriteToLog()
        {
            app_cpuid = string.IsNullOrEmpty(hidAppCPUID.Value) ? 0 : Convert.ToInt32(hidAppCPUID.Value);
            cpuid = string.IsNullOrEmpty(hidTblCPUID.Value) ? 0 : Convert.ToInt32(hidTblCPUID.Value);
            string ChangeInfo = string.Empty;
            if (txtChristianName.Text.Trim() != "")
            {
                ChangeInfo += CPerson(ChangeInfo);
                ChangeInfo += UploadMide(ChangeInfo);
                ChangeInfo += AddressInfo(ChangeInfo);
                ChangeInfo = contactInfo(ChangeInfo);
                ChangeInfo = ChangeInfo.Replace("change to", "add:");
                InsertToHistory(15, "", ChangeInfo);
                ChangeInfo = string.Empty;
                ChangeInfo += JobNatureInfo(ChangeInfo);
                InsertToHistory(16, "Job Job Nature", ChangeInfo);
                ChangeInfo = string.Empty;
                ChangeInfo += JobAttributeInfo(ChangeInfo);
                InsertToHistory(19, "Job Attribute", ChangeInfo);
                ChangeInfo = string.Empty;
                ChangeInfo += CPersonCommMembe(ChangeInfo);
                InsertToHistory(12, "Committee Member", ChangeInfo);
            }
            else
            {
                ChangeInfo += CPerson(ChangeInfo);
                ChangeInfo += UploadMide(ChangeInfo);
                ChangeInfo += AddressInfo(ChangeInfo);
                ChangeInfo = contactInfo(ChangeInfo);
                InsertToHistory(22, "", ChangeInfo);
                ChangeInfo = string.Empty;
                ChangeInfo += JobNatureInfo(ChangeInfo);
                InsertToHistory(17, "Job Job Nature", ChangeInfo);
                ChangeInfo = string.Empty;
                ChangeInfo += JobAttributeInfo(ChangeInfo);
                InsertToHistory(20, "Job Attribute", ChangeInfo);
                ChangeInfo = string.Empty;
                ChangeInfo += CPersonCommMembe(ChangeInfo);
                InsertToHistory(13, "Committee Member", ChangeInfo);
            }
        }
        protected void InsertToHistory(int historytypeUID, string info_prat, string ChangeInfo)
        {
            if (ChangeInfo.Trim().Length > 0)
            {
                BasePage basepage = new BasePage();
                string ChangeLoge = string.Empty;
                string LoginName = tblCust_WebLoginBLL.GetInstance().GetENT(GetMemberLoginUID()).LoginName.ToString();
                Literal CompanyName = usermeg.FindControl("ltlCompanyName_eng") as Literal;
                tblHistory entlog = new tblHistory();
                entlog.HistoryCatUID = 2;
                entlog.HistoryTypeUID = 6;
                entlog.CustomerUID = basepage.GetEnt();
                entlog.Cust_MemberUID = 0;
                entlog.ContactPersonUID = 0;
                entlog.History_Detail = LoginName + " {" + CompanyName.Text + "}{Contact Person " + info_prat + "} " + ChangeInfo;
                entlog.LastModifyBy = LoginName;
                int tal = tblHistoryBLL.GetInstance().InsertENT(entlog);
            }
        }
        /// <summary>
        /// 返回有差别的数据
        /// </summary>
        /// <param name="New">需要取出不一样数据的数组</param>
        /// <param name="Source">需要比对的源数组</param>
        /// <returns></returns>
        private List<int> GetChangeList(List<int> Contrast, List<int> Source)
        {
            List<int> add = new List<int>();
            for (int i = 0; i < Contrast.Count(); i++)
            {
                bool has = Source.Contains(Contrast[i]);
                if (has == false)
                    add.Add(Contrast[i]);
            }
            return add;
        }
        protected string Val(string str)
        {
            if (string.IsNullOrEmpty(str))
                str = "";
            return str;
        }
        #endregion
        //AJAX删除Commitee重新绑定数据
        #region
        protected void btnRefreshCommitee_Click(object sender, EventArgs e)
        {
            app_cpuid = string.IsNullOrEmpty(hidAppCPUID.Value) ? 0 : Convert.ToInt32(hidAppCPUID.Value);
            cpuid = string.IsNullOrEmpty(hidTblCPUID.Value) ? 0 : Convert.ToInt32(hidTblCPUID.Value);

            customerENT = GetCustomerFromSession();
            if (customerENT != null)
            {
                BindCommitteeMember();
                BindCPList();
            }
        }
        protected void btnRefreshImgList_Click(object sender, EventArgs e)
        {
            customerENT = GetCustomerFromSession();
            if (customerENT != null)
            {
                CustomerUID = customerENT.CustomerUID;
                BindCPImg();
            }
            else
            { lblMeg.Text = ResourceManage.GetResourceString("msg200016"); }
        }
        protected void btnRefreshCPList_Click(object sender, EventArgs e)
        {
            customerENT = GetCustomerFromSession();
            if (customerENT != null)
            {
                BindCPList();
            }
        }
        protected void ddlArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCountry();
        }
        protected void rptRelatedImgList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal ltImg = e.Item.FindControl("ltImg") as Literal;
                M_MediaRoomRelatedDetail tempENT = e.Item.DataItem as M_MediaRoomRelatedDetail;
                string KeyValue = e.Item.DataItem as string;
                if (tempENT!=null)
                {                    
                    string[] key =tempENT.KeyMediaRelatedUID.Split('_');
                    string From = key[1];
                    int MediaRoomKeyUID = Convert.ToInt32(key[0]);
                    string strImgUrl = string.Empty;
                    if (From == "app")
                    {
                        app_tblMediaRoom_Related app_related = new app_tblMediaRoom_Related();
                        app_related.AppMediaRoomRelatedUID = MediaRoomKeyUID;
                        app_related = app_relatedBLL.GetListByENT(app_related).FirstOrDefault();
                        if (app_related != null)
                        {
                            if (app_related.AppMediaRoomUID > 0)
                            {
                                app_tblMediaRoom app_room = new app_tblMediaRoom();
                                app_room.AppMediaRoomUID = (int)app_related.AppMediaRoomUID;
                                app_room = app_mediaBLL.GetListByENT(app_room).FirstOrDefault();
                                if (app_room != null)
                                {
                                    strImgUrl = "<img src=\"" + app_room.FilePath + app_room.FileName + "_150_150.jpg \"/>";
                                }
                            }
                            else if (app_related.tblMediaRoom!=null)
                            {
                                var temp = app_related.tblMediaRoom;
                                strImgUrl = "<img src=\"" + temp.FilePath + temp.FileName + "_150_150.jpg \"/>";
                            }
                        }
                    }
                    else
                    {
                        tblMediaRoom_Related tbl_related = new tblMediaRoom_Related();
                        tbl_related.MediaRoomRelatedUID = MediaRoomKeyUID;
                        tbl_related = tbl_relatedBLL.GetListByENT(tbl_related).FirstOrDefault();
                        if (tbl_related != null && tbl_related.tblMediaRoom != null)
                        {
                            var temp = tbl_related.tblMediaRoom;
                            strImgUrl = "<img src=\"" + temp.FilePath + temp.FileName + "_150_150.jpg \"/>";
                        }

                    }
                    ltImg.Text = strImgUrl;
                }
                //if (comm != null)
                //{
                //    ltYear.Text = comm.Year;
                //    ltDate.Text = DateFormat(comm.StartDate) + " to " + DateFormat(comm.EndDate);
                //    ltType.Text = comm.mtblType.CommType_Eng;
                //    ltPosition.Text = comm.mtblPosition.CommPosition_Eng;
                //}
            }
        }
        protected void rptCommitteeMember_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal ltYear = e.Item.FindControl("ltYear") as Literal;
                Literal ltDate = e.Item.FindControl("ltDate") as Literal;
                Literal ltType = e.Item.FindControl("ltType") as Literal;
                Literal ltPosition = e.Item.FindControl("ltPosition") as Literal;
                CommitteeMemberList comm = e.Item.DataItem as CommitteeMemberList;
                if (comm != null)
                {
                    ltYear.Text = comm.Year;
                    ltDate.Text = DateFormat(comm.StartDate) + " to " + DateFormat(comm.EndDate);
                    ltType.Text = comm.mtblType.CommType_Eng;
                    ltPosition.Text = comm.mtblPosition.CommPosition_Eng;
                }
            }
        }
        #endregion
    }
}